Android(安卓)图片大小超过预算的VM:java.lang.OutOfMemoryError: bitmap size exceeds VM budget
16lz
2021-01-24
Android 2.3.3 Eclipse Version: 3.7.0 LogCat
Activity显示图片,部分源代码:
// 图片try {bitmap = BitmapFactory.decodeFile("/" + sdpath+ htc.getBitmap(), opt);} catch (Exception e) {e.printStackTrace();}htcImageView.setImageBitmap(bitmap);
使用过程中
LogCat 报错信息:
02-07 17:04:02.947: DEBUG/dalvikvm(15660): GC_EXTERNAL_ALLOC freed 75K, 61% free 3080K/7751K, external 14227K/16275K, paused 100ms02-07 17:04:03.017: ERROR/dalvikvm-heap(15660): 4147200-byte external allocation too large for this process.02-07 17:04:03.137: ERROR/GraphicsJNI(15660): VM won't let us allocate 4147200 bytes02-07 17:04:03.148: DEBUG/dalvikvm(15660): GC_FOR_MALLOC freed 2K, 61% free 3078K/7751K, external 14227K/16275K, paused 37ms02-07 17:04:03.191: DEBUG/skia(15660): --- decoder->decode returned false02-07 17:04:03.191: DEBUG/AndroidRuntime(15660): Shutting down VM02-07 17:04:03.191: WARN/dalvikvm(15660): threadid=1: thread exiting with uncaught exception (group=0x40015560)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): FATAL EXCEPTION: main02-07 17:04:03.218: ERROR/AndroidRuntime(15660): java.lang.OutOfMemoryError: bitmap size exceeds VM budget02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:284)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at com.taobao.htc.Game.handerUI(Game.java:522)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at com.taobao.htc.Game$1.run(Game.java:396)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at android.os.Handler.handleCallback(Handler.java:587)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at android.os.Handler.dispatchMessage(Handler.java:92)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at android.os.Looper.loop(Looper.java:123)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at android.app.ActivityThread.main(ActivityThread.java:3683)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at java.lang.reflect.Method.invokeNative(Native Method)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at java.lang.reflect.Method.invoke(Method.java:507)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)02-07 17:04:03.218: ERROR/AndroidRuntime(15660): at dalvik.system.NativeStart.main(Native Method)02-07 17:04:03.268: WARN/ActivityManager(61): Force finishing activity com.taobao.htc/.Game
发生错误原因分析:
bitmap图片尺寸较大,使用后超过预算的VM;
网上查找到两种解决办法:
一种是使用缩略图,缩小图片大小
opt = new BitmapFactory.Options();……opt.inSampleSize = 2; //使用缩略图,缩小倍数
另一种是增加VM大小
VMRuntime.getRuntime().setMinimumHeapSize(16 * 1024 * 1024); VMRuntime.getRuntime().setTargetHeapUtilization(0.75f);
但测试无效。
最好的解决办法:显示的回收图片。
更多相关文章
- Android实现ImageView图片双击放大及缩小
- 高仿大众点评商家列表
- android拍照后bitmap显示问题
- Android(安卓)中使用 AsyncTask 异步读取网络图片
- android 压缩图片、生成缩略图 和转换Base64格式的字符串
- android 图片的 放大 缩小 移动
- Android手势控制实现缩放、移动图片
- Android源码大放送(实战开发必备)
- Android(安卓)RecycerView 中根据图片大小自适应控件大小的实现