properly recycle bitmaps

This commit is contained in:
Daniel Gultsch 2015-10-02 11:39:30 +02:00
parent 2b9b700c96
commit 4b62bd256d

View file

@ -112,7 +112,11 @@ public class FileBackend {
scalledW = size; scalledW = size;
scalledH = (int) (h / ((double) w / size)); scalledH = (int) (h / ((double) w / size));
} }
return Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true); Bitmap result = Bitmap.createScaledBitmap(originalBitmap, scalledW, scalledH, true);
if (originalBitmap != null && !originalBitmap.isRecycled()) {
originalBitmap.recycle();
}
return result;
} else { } else {
return originalBitmap; return originalBitmap;
} }
@ -123,7 +127,11 @@ public class FileBackend {
int h = bitmap.getHeight(); int h = bitmap.getHeight();
Matrix mtx = new Matrix(); Matrix mtx = new Matrix();
mtx.postRotate(degree); mtx.postRotate(degree);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true); Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
}
return result;
} }
public boolean useImageAsIs(Uri uri) { public boolean useImageAsIs(Uri uri) {
@ -227,7 +235,6 @@ public class FileBackend {
if (rotation > 0) { if (rotation > 0) {
scaledBitmap = rotate(scaledBitmap, rotation); scaledBitmap = rotate(scaledBitmap, rotation);
} }
boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, Config.IMAGE_QUALITY, os); boolean success = scaledBitmap.compress(Config.IMAGE_FORMAT, Config.IMAGE_QUALITY, os);
if (!success) { if (!success) {
throw new FileCopyException(R.string.error_compressing_image); throw new FileCopyException(R.string.error_compressing_image);
@ -288,7 +295,6 @@ public class FileBackend {
throw new FileNotFoundException(); throw new FileNotFoundException();
} }
thumbnail = resize(fullsize, size); thumbnail = resize(fullsize, size);
fullsize.recycle();
int rotation = getRotation(file); int rotation = getRotation(file);
if (rotation > 0) { if (rotation > 0) {
thumbnail = rotate(thumbnail, rotation); thumbnail = rotate(thumbnail, rotation);
@ -447,6 +453,9 @@ public class FileBackend {
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888); Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(dest); Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null); canvas.drawBitmap(source, null, targetRect, null);
if (source != null && !source.isRecycled()) {
source.recycle();
}
return dest; return dest;
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
return null; return null;
@ -470,6 +479,9 @@ public class FileBackend {
Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888); Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output); Canvas canvas = new Canvas(output);
canvas.drawBitmap(input, null, target, null); canvas.drawBitmap(input, null, target, null);
if (input != null && !input.isRecycled()) {
input.recycle();
}
return output; return output;
} }