Android bitmap image size

2019-04-07 14:10发布

I am downloading images from web and i use Gallery widget to display the images.

If the downloaded image size is huge, my application crashes with the below log.

"E/GraphicsJNI( 3378): VM won't let us allocate 5591040 bytes"

I want to scale down the downloaded image size only when the image size is more to an extent that it will crash the app. I have written the code to scale down the image size but i am not sure how to find the bitmap size so i can decide on whether to scale or not

   BitmapFactory.Options o = new BitmapFactory.Options();
   o.inSampleSize = 2;
   Bitmap bit = BitmapFactory.decodeStream(inputStream,null,o);
   Bitmap scaled = Bitmap.createScaledBitmap(bit, 200, 200, true);
   bit.recycle();
   return scaled;

2条回答
聊天终结者
2楼-- · 2019-04-07 14:28

Use inJustDecodeBounds field of BitmapFactory.Options to get bitmap dimensions.

查看更多
做个烂人
3楼-- · 2019-04-07 14:44

To get bitmap dimensions, you can simply use:

To get height -> bitmap.getHeight() To get width -> bitmap.getWidth()

查看更多
登录 后发表回答