在Textview中显示位图大小



我有这个问题。我使用decodeByteArray来解码和解密数据作为图像,之后我试图获得实际大小(以字节为单位,而不是宽度&,并在textview中显示。

Bitmap bitmap = BitmapFactory.decodeByteArray(decryptedData , 0, decryptedData .length);    //decoding bytearrayoutputstream to bitmap
int i = bitmap.getRowBytes() * bitmap.getHeight() ;
TextView txt = (TextView) findViewById(R.id.text);
txt.setText(i);

代码正在运行没有任何错误,但大小没有显示在TextView。有什么建议吗?

请试试这个,告诉我结果

Bitmap bitmap = BitmapFactory.decodeByteArray(decryptedData , 0, decryptedData .length); 
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
TextView txt = (TextView) findViewById(R.id.text);
txt.setText(String.valueOf(stream.size())); 

TextView.setText(int resId)不应该这样使用。i应该是一个资源id,如R.string.my_string_here

你真正想要的是

txt.setText(String.valueOf(i));

这一行:

int i = bitmap.getRowBytes() * bitmap.getHeight() ;

你应该使用getWidth()而不是GetRowBytes(),因为GetRowBytes()返回位图像素行之间的字节数,而getWidth()返回图像的宽度。

try this

  ByteArrayOutputStream stream = new ByteArrayOutputStream();
  bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
  byte[] byteArray = stream.toByteArray();
  BitmapFactory.Options bounds = new BitmapFactory.Options(); 
  BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);
  int width = bounds.outWidth;
  int height = bounds.outHeight;

最新更新