Android 圖片大小超過預算的VM:java.lang.OutOfMemoryError: bitmap size exceeds VM budget

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);


但測試無效。

最好的解決辦法:顯示的回收圖片。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章