2011-02-26 24 views
0

您好我想在我的視圖中顯示存儲在sdcard中的3或4張圖像,圖像大小約爲1-2 MB。 我的問題是,當我使用imageview的圖像,然後扔出去內存異常 我已創建位,並通過選擇解碼圖像爲位圖如何在android中爲imageview壓縮圖像

02-26 13:16:54.946: ERROR/dalvikvm-heap(23410): 15980544-byte external allocation too large for this process. 
02-26 13:16:54.946: ERROR/dalvikvm(23410): Out of memory: Heap Size=3407KB, Allocated=2801KB, Bitmap Size=15630KB, Limit=21884KB 
02-26 13:16:54.946: ERROR/dalvikvm(23410): Trim info: Footprint=3463KB, Allowed Footprint=3655KB, Trimmed=248KB 
02-26 13:16:54.946: ERROR/GraphicsJNI(23410): VM won't let us allocate 15980544 bytes 
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): FATAL EXCEPTION: main 
02-26 13:16:54.986: ERROR/AndroidRuntime(23410): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
02-26 13:16:54.986: ERROR/AndroidRuntime(23410):  at android.graphics.BitmapFactory.nativeDecodeFile(Native Method) 
02-26 13:16:54.986: ERROR/AndroidRuntime(23410):  at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:325) 

任何機構可以幫助我解決我的問題提前 感謝時

回答

5

我發現我的問題的解決方案,這正是我的代碼:

BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inTempStorage = new byte[24*1024]; 
options.inJustDecodeBounds = false; 
options.inSampleSize=32;  
bmp1=BitmapFactory.decodeFile(filepath1,options); 
Bitmap b1=ThumbnailUtils.extractThumbnail(bmp1,30, 30); 
iv1.setImageBitmap(b); 
if(bmp1!=null){ 
    bmp1.recycle(); 
    } 
     bmp1=BitmapFactory.decodeFile(filepath1,options); 
Bitmap b2=ThumbnailUtils.extractThumbnail(bmp1,30, 30); 
iv2.setImageBitmap(b2); 
if(bmp1!=null){ 
bmp1.recycle(); 
} 

同樣我也用它來做四個圖像查看和設置圖像沒有OOM異常

1

創建一個BitmapFactory.Options並在inSampleSize中傳遞一個大於1的值(最好是2的冪),以便在加載圖像時縮小圖像的大小。

+0

我創建選項BitmapFactory.Options選項= new BitmapFactory.Options(); \t \t \t options.inTempStorage = new byte [16 * 1024]; \t \t \t options.inJustDecodeBounds = false; \t \t \t options.inSampleSize = 8; – 2011-02-26 08:45:20

+0

你是說這解決了你的問題,或者你是否已經失去了記憶? – EboMike 2011-02-26 15:25:47

+0

@EboMike:thanx幫助我。我說它不能解決我的問題,當我在我的代碼中使用BitmapFactory選項時,它仍會拋出OOM異常。 – 2011-02-28 09:44:21