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 100ms 02-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 bytes 02-07 17:04:03.148: DEBUG/dalvikvm(15660): GC_FOR_MALLOC freed 2K, 61% free 3078K/7751K, external 14227K/16275K, paused 37ms 02-07 17:04:03.191: DEBUG/skia(15660): --- decoder->decode returned false 02-07 17:04:03.191: DEBUG/AndroidRuntime(15660): Shutting down VM 02-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: main 02-07 17:04:03.218: ERROR/AndroidRuntime(15660): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 02-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);
但测试无效。
最好的解决办法:显示的回收图片。