我使用这个功能来旋转从摄像机位图或画廊: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();
}
}
}
任何人都可以想办法解决这个错误?
尽量不要在内存中存储如此多的位图 –