From f0456e1a41db3107fc1d8387295ef4274c60dea4 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 31 Jul 2019 09:27:50 +0200 Subject: [PATCH] Use temporary file for resizing images --- .../eu/faircode/email/FragmentCompose.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index fc172ce028..b3e790bd06 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1903,25 +1903,29 @@ public class FragmentCompose extends FragmentBase { options.inJustDecodeBounds = false; options.inSampleSize = factor; - Bitmap scaled = BitmapFactory.decodeFile(file.getAbsolutePath(), options); - if (scaled != null) { - Log.i("Image target size=" + scaled.getWidth() + "x" + scaled.getHeight() + " rotation=" + rotation); + Bitmap resized = BitmapFactory.decodeFile(file.getAbsolutePath(), options); + if (resized != null) { + Log.i("Image target size=" + resized.getWidth() + "x" + resized.getHeight() + " rotation=" + rotation); if (rotation != null) { - Bitmap rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.getWidth(), scaled.getHeight(), rotation, true); - scaled.recycle(); - scaled = rotated; + Bitmap rotated = Bitmap.createBitmap(resized, 0, 0, resized.getWidth(), resized.getHeight(), rotation, true); + resized.recycle(); + resized = rotated; } - try (OutputStream out = new FileOutputStream(file)) { - scaled.compress("image/jpeg".equals(attachment.type) + File tmp = File.createTempFile(Long.toString(attachment.id), ".resized", context.getCacheDir()); + try (OutputStream out = new FileOutputStream(tmp)) { + resized.compress("image/jpeg".equals(attachment.type) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG, REDUCED_IMAGE_QUALITY, out); } finally { - scaled.recycle(); + resized.recycle(); } + file.delete(); + tmp.renameTo(file); + DB db = DB.getInstance(context); db.attachment().setDownloaded(attachment.id, file.length()); }