Android 位图图像大小
我正在从网络下载图像,并使用图库小部件来显示图像。
如果下载的图像很大,我的应用程序会崩溃并显示以下日志。
"E/GraphicsJNI( 3378): VM won't let us allocate 5591040 bytes"
仅当图像大小达到会使应用程序崩溃的程度时,我才想缩小下载的图像大小。我已经编写了缩小图像大小的代码,但我不确定如何找到位图大小,以便我可以决定是否缩放
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;
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要获取位图尺寸,您可以简单地使用:
获取高度 ->位图.getHeight()
获取宽度 ->位图.getWidth()
To get bitmap dimensions, you can simply use:
To get height -> bitmap.getHeight()
To get width -> bitmap.getWidth()
使用
BitmapFactory.Options< 的 inJustDecodeBounds 字段/code> 获取位图尺寸。
Use inJustDecodeBounds field of
BitmapFactory.Options
to get bitmap dimensions.