Problemas de memória fora do bitmap
Meu problema
Eu tiro uma foto com o meu dispositivo android. Eu então decodifico essa imagem do arquivo.
Bitmap photo = BitmapFactory.decodeFile(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
if (photo == null && data != null)
photo = (Bitmap) data.getExtras().get("data");
else if (data == null && photo == null)
Log.e("CCPhotoManager","Can't find image from file or from intent data.");
Eu então verifico essa imagem e vejo se ela precisa ser girada para a orientação correta.
try {
ExifInterface exif = new ExifInterface(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX);
int rotation = CCDataUtils.exifToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_NORMAL));
Log.v("CCPhotoManager", "Rotation:"+rotation);
if (rotation > 0) {
photo = this.convertSavedImageToCorrectOrientation(EXTERNAL_IMAGE_PATH+File.separator+this._currentPhotoName+JPEG_FILE_SUFFIX, photo, rotation);
}
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Se precisar de rotação, eu chamo esse método.
public Bitmap convertSavedImageToCorrectOrientation(String filePath,Bitmap photo,int rotation) {
Log.d("CCPhotoManager", "Changing Orientation of photo located at: "+filePath+" Rotating by:"+rotation);
int width = photo.getWidth();
int height = photo.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(rotation);
Bitmap adjusted = Bitmap.createBitmap(photo, 0, 0, width, height, matrix, true);
try {
FileOutputStream out = new FileOutputStream(filePath);
adjusted.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
return adjusted;
}
estou obtendoFora da memória reclamações se oconvertSavedImageToCorrectOrientation
é chamado na linhaBitmap adjusted = Bitmap.createBitmap(photo,0,0,width,height,matrix,true);
Este é apenas o caso noSamsung Galaxy S3
. Funciona bem noSamsung Galaxy Ace
, HTC Hero
e aSony Xperia U
.
Aqui está o erro.
10-17 14:33:33.950: E/AndroidRuntime(12556): java.lang.OutOfMemoryError
10-17 14:33:33.950: E/AndroidRuntime(12556): at android.graphics.Bitmap.nativeCreate(Native Method)
10-17 14:33:33.950: E/AndroidRuntime(12556): at android.graphics.Bitmap.createBitmap(Bitmap.java:605)
10-17 14:33:33.950: E/AndroidRuntime(12556): at android.graphics.Bitmap.createBitmap(Bitmap.java:551)
É uma enorme quantidade de memória também.
10-17 14:33:33.945: E/dalvikvm-heap(12556): Out of memory on a 31961104-byte allocation.
Eu acho que é algo a ver com a quantidade de Bitmaps ao redor, mas não tenho certeza de como impedir que esse erro aconteça.
Eu sei que você pode ligar.recycle();
sobre eles, mas não parece funcionar.
Minha pergunta
Como faço para lidar corretamente com meus bitmaps para que eu não tenha esse problema de OOM?
desde já, obrigado