android-crop 라이브러리를 사용 중입니다. 사용자가 큰 이미지 (전화 카메라의 이미지)에서 자르려고하면 메모리 부족으로 인해 절망적입니다.비트 맵 압축시 OutOfMemoryError
결과 비트 맵을 압축하려고했지만 비트 맵을 압축하려고 할 때 여전히 OutOfMemoryError
이 표시됩니다. 여기에 내 코드가 간다 : 내가 뭘 잘못하고 있고/또는 어떻게 메모리 오류가 발생하지 않도록 할 수 있는지 알려주시겠습니까? 나는이 문제를과 같이 해결할 수있는 게이브의 관점에
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
if (mBitmap != null) {
mBitmap.recycle();
mBitmap = null;
}
Bitmap temp; //temporary bitmap
try {
temp = MediaStore.Images.Media.getBitmap(getContentResolver()
, Crop.getOutput(result)); //load image from URI
ByteArrayOutputStream out = new ByteArrayOutputStream();
temp.compress(Bitmap.CompressFormat.JPEG, 60, out);
mBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
out.close();
temp.recycle();
temp = null;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "error loading photo. please try with another photo"
, Toast.LENGTH_SHORT).show();
return;
}
if (mBitmap != null) {
mImageViewProfilePhoto.setImageBitmap(mBitmap);
enableFinalization();
}
}
}
감사 :
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
if (mBitmap != null) {
mBitmap.recycle();
mBitmap = null;
}
try {
mBitmap = MediaStore.Images.Media.getBitmap(getContentResolver()
, Crop.getOutput(result)); //load image from URI
File tempImageFile = new File(mContext.getFilesDir().getAbsolutePath()
, "temp_1311_14_hahahah_lol_WTF_shit_1414.bin");
FileOutputStream out = new FileOutputStream (tempImageFile);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 30, out);
mBitmap.recycle();
mBitmap = null;
mBitmap = BitmapFactory.decodeFile(tempImageFile.getPath());
boolean deleted = tempImageFile.delete();
out.close();
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "error loading photo. please try with another photo"
, Toast.LENGTH_SHORT).show();
return;
}
if (mBitmap != null) {
mImageViewProfilePhoto.setImageBitmap(mBitmap);
enableFinalization();
}
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
disableFinalization();
}
}
은 다른 사람을 위해 도움이 될 것입니다 바랍니다.
감사합니다. 임시 비트 맵을 제거하고 파일 스트림 insted를 사용하여 문제가 해결되었습니다! –