2013-10-10 32 views
0

我有199KB的圖像存儲在內部存儲器中。我的設備的總Ram是512 MB。 Android版本是2.3.5。所以我正在做的是我在運行時將所有圖像繪製爲位圖。Android內存不足綁定例外

我得到以下異常:

10-10 14:16:03.782: E/dalvikvm-heap(20331): 73632-byte external allocation too large for this process. 
10-10 14:16:03.782: E/dalvikvm(20331): Out of memory: Heap Size=6599KB, Allocated=3949KB, Bitmap Size=13871KB, Limit=20480KB 
10-10 14:16:03.782: E/dalvikvm(20331): Trim info: Footprint=6599KB, Allowed Footprint=6599KB, Trimmed=264KB 
10-10 14:16:03.792: E/GraphicsJNI(20331): VM won't let us allocate 73632 bytes 

這裏是我的代碼:

File myDir =act.getApplicationContext().getDir(CURRENT_THEME, act.MODE_PRIVATE); 
public BitmapDrawable getResourceFromInternalStorage(String resourceName) 
{ 
    // Runtime.getRuntime().gc(); 
    String filename = resourceName; 
     File file = new File(myDir, filename+".png"); 
    Resources res = act.getResources(); 
    BitmapDrawable bd =null; 
    bitmap=null; 
    try 
    { 
// BitmapFactory.Options options = new BitmapFactory.Options(); 
// options.inJustDecodeBounds = true; 
// options.inSampleSize = 32; 


       BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
        bmpFactoryOptions.inJustDecodeBounds=true; 
        BitmapFactory.decodeFile(file.getPath(),bmpFactoryOptions); 

        long reqsize=bmpFactoryOptions.outWidth*bmpFactoryOptions.outHeight*2; 
        long allocNativeHeap = Debug.getNativeHeapAllocatedSize(); 


        final long heapPad=(long) Math.max(4*1024*1024,Runtime.getRuntime().maxMemory()*0.1); 
        if ((reqsize + allocNativeHeap + heapPad) >= Runtime.getRuntime().maxMemory()) 
        { 
        Toast.makeText(act, "exception", Toast.LENGTH_LONG).show(); 
//      bitmap.recycle(); 
        // Runtime.getRuntime().gc(); 
//     System.gc(); 
        // return null; 

        } 
        bmpFactoryOptions.inJustDecodeBounds=false; 
        bitmap=BitmapFactory.decodeFile(file.getPath(),bmpFactoryOptions); 
      bd = new BitmapDrawable(res, bitmap); 
    } 
    catch(java.lang.OutOfMemoryError e) 
    { 
    Log.v("Exceptional exception", e.getMessage()); 
    bitmap.recycle(); 
    System.gc(); 
    } 
    finally{ 

    } 

     return bd; 
} 
+0

我收到同樣的問題,前一段日子,但我通過仿真器分配更多的內存的大小來解決它。 – DJhon

+0

@BlueGreen我不能在設備上爲ram分配更多尺寸,我的目標不是模擬器,它的設備 – user2137186

回答

1

做你的位圖的循環。你這樣做,但在catch塊。

在try塊

bitmap.recycle(); 
bitmap=null; 

嘗試上面的代碼和檢查。

+0

我已經嘗試過,但在try塊位圖中爲null。所以它在回收時拋出異常。 – user2137186

+0

做一件事情把條件,如果(位圖!= null {bitmap.recycle();位圖= null;} –

+0

我仍然收到一個異常 – user2137186