From aada937c74c9a0d1ba56aa8d81dde8ca7443e943 Mon Sep 17 00:00:00 2001 From: M66B Date: Mon, 27 Apr 2020 09:54:17 +0200 Subject: [PATCH] Improved image caching --- .../java/eu/faircode/email/FragmentCompose.java | 6 +++--- .../main/java/eu/faircode/email/ImageHelper.java | 13 +++++++++---- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 727beada6f..6f868433e7 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -1882,7 +1882,7 @@ public class FragmentCompose extends FragmentBase { new SimpleTask() { @Override protected Spanned onExecute(Context context, Bundle args) throws IOException { - long id = args.getLong("id"); + final long id = args.getLong("id"); List uris = args.getParcelableArrayList("uris"); boolean image = args.getBoolean("image"); int resize = args.getInt("resize"); @@ -2637,7 +2637,7 @@ public class FragmentCompose extends FragmentBase { Spanned spanned = HtmlHelper.fromHtml(text, new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { - return ImageHelper.decodeImage(getContext(), -1, source, true, 0, etBody); + return ImageHelper.decodeImage(getContext(), working, source, true, 0, etBody); } }, null); etBody.getText().insert(etBody.getSelectionStart(), spanned); @@ -4572,7 +4572,7 @@ public class FragmentCompose extends FragmentBase { signature = HtmlHelper.fromHtml(identity.signature, new Html.ImageGetter() { @Override public Drawable getDrawable(String source) { - return ImageHelper.decodeImage(getContext(), -1, source, true, 0, tvSignature); + return ImageHelper.decodeImage(getContext(), working, source, true, 0, tvSignature); } }, null); tvSignature.setText(signature); diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 469f5764ea..fe68701ed5 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -333,7 +333,7 @@ class ImageHelper { return d; } - if (!show || id < 0) { + if (!show) { // Show placeholder icon int resid = (embedded || data ? R.drawable.baseline_photo_library_24 : R.drawable.baseline_image_24); Drawable d = res.getDrawable(resid, theme); @@ -513,6 +513,9 @@ class ImageHelper { } private static Drawable getCachedImage(Context context, long id, String source) { + if (id < 0) + return null; + File file = getCacheFile(context, id, source); if (file.exists()) { Log.i("Using cached " + file); @@ -618,9 +621,11 @@ class ImageHelper { Log.i("Downloaded image source=" + source); - File file = getCacheFile(context, id, source); - try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { - bm.compress(Bitmap.CompressFormat.PNG, 90, os); + if (id >= 0) { + File file = getCacheFile(context, id, source); + try (OutputStream os = new BufferedOutputStream(new FileOutputStream(file))) { + bm.compress(Bitmap.CompressFormat.PNG, 90, os); + } } Drawable d = new BitmapDrawable(res, bm);