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;
 
ネット上には2つの解決策があります.1つはVMサイズを増やすことです.
		 VMRuntime.getRuntime().setMinimumHeapSize(12 * 1024 * 1024);
		 VMRuntime.getRuntime().setTargetHeapUtilization(0.85f);

個人テストで無効です.
 
もう1つはサムネイルを使って、画像のサイズを縮小することです
		//      
		opt.inSampleSize = 2; 

実現できる.