From c8be990d1d6ada5fa1568588f15157ebd39458e7 Mon Sep 17 00:00:00 2001 From: Vavassor Date: Tue, 2 May 2017 20:06:48 -0400 Subject: [PATCH] Fixes a crash when reorienting and downsizing an image for upload. Closes #247 --- .../keylesspalace/tusky/DownsizeImageTask.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/DownsizeImageTask.java b/app/src/main/java/com/keylesspalace/tusky/DownsizeImageTask.java index 4e0d77db..56d50ed9 100644 --- a/app/src/main/java/com/keylesspalace/tusky/DownsizeImageTask.java +++ b/app/src/main/java/com/keylesspalace/tusky/DownsizeImageTask.java @@ -86,7 +86,9 @@ class DownsizeImageTask extends AsyncTask { try { Bitmap result = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); - bitmap.recycle(); + if (!bitmap.sameAs(result)) { + bitmap.recycle(); + } return result; } catch (OutOfMemoryError e) { return null; @@ -180,17 +182,21 @@ class DownsizeImageTask extends AsyncTask { if (scaledBitmap == null) { return false; } - reorientBitmap(scaledBitmap, orientation); + Bitmap reorientedBitmap = reorientBitmap(scaledBitmap, orientation); + if (reorientedBitmap == null) { + scaledBitmap.recycle(); + return false; + } Bitmap.CompressFormat format; /* It's not likely the user will give transparent images over the upload limit, but * if they do, make sure the transparency is retained. */ - if (!scaledBitmap.hasAlpha()) { + if (!reorientedBitmap.hasAlpha()) { format = Bitmap.CompressFormat.JPEG; } else { format = Bitmap.CompressFormat.PNG; } - scaledBitmap.compress(format, 75, stream); - scaledBitmap.recycle(); + reorientedBitmap.compress(format, 75, stream); + reorientedBitmap.recycle(); scaledImageSize /= 2; iterations++; } while (stream.size() > sizeLimit);