我使用這個功能來旋轉從攝像機位圖或畫廊: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();
}
}
}
任何人都可以想辦法解決這個錯誤?
儘量不要在內存中存儲如此多的位圖 –