使用bitmap缩略图,解决图片大小超过预算的VM的问题

Android 2.3.3         
Eclipse Version: 3.7.0         
LogCat 

 

Activity中显示大图,部分源代码如下:

			try {
				bitmap = BitmapFactory.decodeFile("/" + nokia.getBitmap(), opt);
			} catch (Exception e) {
				e.printStackTrace();
			}
			nokiaImage.setImageBitmap(bitmap);

 
使用过程中
LogCat 报错信息:

02-07 13:14:22.947: DEBUG/dalvikvm(33246): GC_EXTERNAL_ALLOC freed 75K, 61% free 3080K/7751K, external 14227K/16275K, paused 100ms
02-07 13:14:23.017: ERROR/dalvikvm-heap(33246): 4147200-byte external allocation too large for this process.
02-07 13:14:23.137: ERROR/GraphicsJNI(33246): VM won't let us allocate 4147200 bytes
02-07 13:14:23.148: DEBUG/dalvikvm(33246): GC_FOR_MALLOC freed 2K, 61% free 3078K/7751K, external 14227K/16275K, paused 37ms
02-07 13:14:23.191: DEBUG/skia(33246): --- decoder->decode returned false
02-07 13:14:23.191: DEBUG/AndroidRuntime(33246): Shutting down VM
02-07 13:14:23.191: WARN/dalvikvm(33246): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): FATAL EXCEPTION: main
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:470)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:284)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.tmall.nokia.Book.handerUI(Book.java:522)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.tmall.nokia.Book$1.run(Book.java:396)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.os.Handler.handleCallback(Handler.java:587)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.os.Looper.loop(Looper.java:123)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at java.lang.reflect.Method.invokeNative(Native Method)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at java.lang.reflect.Method.invoke(Method.java:507)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-07 13:14:23.218: ERROR/AndroidRuntime(33246): at dalvik.system.NativeStart.main(Native Method)
02-07 13:14:23.268: WARN/ActivityManager(61): Force finishing activity com.tmall.nokia/.Book

 
发生错误的原因分析:
主要是bitmap图片尺寸较大,使用时超过预算的VM;

 

网上有两种解决办法:
一种是增加VM大小

		 VMRuntime.getRuntime().setMinimumHeapSize(12 * 1024 * 1024);
		 VMRuntime.getRuntime().setTargetHeapUtilization(0.85f);

经个人测试无效;

 

另一种是使用缩略图,缩小图片的尺寸大小

		//图片缩小倍数
		opt.inSampleSize = 2; 

可以实现。

你可能感兴趣的:(eclipse,android,VM,DebugLog)