2016-07-25 48 views
0

我使用這個功能來旋轉從攝像機位圖或畫廊:Android Studio中的位圖分配了內存不足的錯誤

public static Bitmap fixOrientation(Bitmap mBitmap) { 

    if (mBitmap.getWidth() > mBitmap.getHeight()) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(90); 
     return Bitmap.createBitmap(mBitmap , 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true); // the error is here! 
    } 
    return mBitmap; 
} 

其workes在頭兩次我使用它細,但在第三次崩潰的應用程序,並給我這個錯誤:

java.lang.OutOfMemoryError: Failed to allocate a 36578316 byte allocation with 16771872 free bytes and 29MB until OOM 

這是這個函數被調用:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (resultCode == RESULT_OK && data != null) { 

     Uri uri = data.getData(); 

     try { 

      Bitmap sourceBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); 

      Bitmap correctBitmap = fixOrientation(sourceBitmap); 
      image.setImageBitmap(correctBitmap); 

      bitmapsArray[cameraSideInt] = correctBitmap; 
      chooseImageLayout.setVisibility(View.GONE); 
      // show change layout 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

任何人都可以想辦法解決這個錯誤?

+0

儘量不要在內存中存儲如此多的位圖 –

回答

-1

是的 - 不要保存那麼多位圖。你將它們存儲在一個數組中。位圖佔用大量內存。當他們在這個陣列中時,他們不能被垃圾收集,所以內存就會丟失。你可能不應該這樣做。

您可以在應用程序中查找其他內存泄漏,它們可能存在並且可能會節省足夠的內存以使其可行。但它是一個壞主意,尤其是如果位圖很大(接近全屏的任何東西)。

+0

好吧,那麼如何保存位圖?我從用戶(相機或畫廊)獲取圖像,並且我正在進行多項活動,所以我需要一種方法來存儲我得到的圖像。 – Rom

+0

活動之間?將它們存儲到磁盤並傳遞文件名。 –

+0

謝謝,修復它! – Rom

相關問題