From 436c686bf5e1a762752c763bda0c52ab11722cb4 Mon Sep 17 00:00:00 2001 From: M66B Date: Thu, 19 Aug 2021 08:22:57 +0200 Subject: [PATCH] Revert "Use image decoder for bitmaps" This reverts commit f9e69b0919241ec15bd040f8d520156c066153cd. --- .../eu/faircode/email/FragmentCompose.java | 23 +----- .../java/eu/faircode/email/ImageHelper.java | 70 ++++++------------- app/src/main/res/values/strings.xml | 1 + 3 files changed, 24 insertions(+), 70 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/FragmentCompose.java b/app/src/main/java/eu/faircode/email/FragmentCompose.java index 0e80cd29fa..a5b20916c0 100644 --- a/app/src/main/java/eu/faircode/email/FragmentCompose.java +++ b/app/src/main/java/eu/faircode/email/FragmentCompose.java @@ -45,7 +45,6 @@ import android.database.MatrixCursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Color; -import android.graphics.ImageDecoder; import android.graphics.Matrix; import android.graphics.Typeface; import android.graphics.drawable.Drawable; @@ -2698,7 +2697,6 @@ public class FragmentCompose extends FragmentBase { args.putParcelableArrayList("uris", new ArrayList<>(uris)); args.putBoolean("image", image); args.putInt("resize", resize); - args.putInt("zoom", zoom); args.putBoolean("privacy", privacy); args.putCharSequence("body", etBody.getText()); args.putInt("start", etBody.getSelectionStart()); @@ -2710,7 +2708,6 @@ public class FragmentCompose extends FragmentBase { List uris = args.getParcelableArrayList("uris"); boolean image = args.getBoolean("image"); int resize = args.getInt("resize"); - int zoom = args.getInt("zoom"); boolean privacy = args.getBoolean("privacy"); CharSequence body = args.getCharSequence("body"); int start = args.getInt("start"); @@ -2731,23 +2728,9 @@ public class FragmentCompose extends FragmentBase { File file = attachment.getFile(context); Uri cid = Uri.parse("cid:" + BuildConfig.APPLICATION_ID + "." + attachment.id); - Drawable d; - try { - if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.P) { - ImageDecoder.Source source = ImageDecoder.createSource(file.getAbsoluteFile()); - d = ImageDecoder.decodeDrawable(source); - } else - d = Drawable.createFromPath(file.getAbsolutePath()); - } catch (Throwable ex) { - Log.w(ex); - d = Drawable.createFromPath(file.getAbsolutePath()); - } - - if (d == null) { - int px = Helper.dp2pixels(context, (zoom + 1) * 24); - d = context.getDrawable(R.drawable.twotone_broken_image_24); - d.setBounds(0, 0, px, px); - } + Drawable d = Drawable.createFromPath(file.getAbsolutePath()); + if (d == null) + throw new IllegalArgumentException(context.getString(R.string.title_no_image)); s.insert(start, "\n\uFFFC\n"); // Object replacement character ImageSpan is = new ImageSpan(context, cid); diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 25dcba9348..ba6c218710 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -795,12 +795,9 @@ class ImageHelper { static Bitmap getScaledBitmap(InputStream is, String source, String mimeType, int scaleToPixels) throws IOException { if (TextUtils.isEmpty(mimeType)) mimeType = Helper.guessMimeType(source); - if ("image/svg+xml".equals(mimeType)) return ImageHelper.renderSvg(is, Color.WHITE, scaleToPixels); - // ImageDecoder cannot decode streams - BufferedInputStream bis = new BufferedInputStream(is); Log.i("Probe " + source); @@ -843,63 +840,36 @@ class ImageHelper { private static Bitmap _decodeImage(File file, String mimeType, int scaleToPixels) throws IOException { if (mimeType == null) mimeType = Helper.guessMimeType(file.getName()); - if ("image/svg+xml".equals(mimeType)) try (FileInputStream fis = new FileInputStream(file)) { return ImageHelper.renderSvg(fis, Color.WHITE, scaleToPixels); } - Bitmap bm = null; - - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) - try { - ImageDecoder.Source isource = ImageDecoder.createSource(file); - bm = ImageDecoder.decodeBitmap(isource, new ImageDecoder.OnHeaderDecodedListener() { - @Override - public void onHeaderDecoded( - @NonNull ImageDecoder decoder, - @NonNull ImageDecoder.ImageInfo info, - @NonNull ImageDecoder.Source source) { - int factor = 1; - while (info.getSize().getWidth() / factor > scaleToPixels) - factor *= 2; - - Log.i("Decode image (decoder) factor=" + factor); - decoder.setTargetSampleSize(factor); - } - }); - } catch (Throwable ex) { - Log.i(ex); - } + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(file.getAbsolutePath(), options); - if (bm == null) { - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(file.getAbsolutePath(), options); - - int factor = 1; - while (options.outWidth / factor > scaleToPixels) - factor *= 2; - - if (factor > 1) { - Log.i("Decode image (factory) factor=" + factor); - options.inJustDecodeBounds = false; - options.inSampleSize = factor; - bm = BitmapFactory.decodeFile(file.getAbsolutePath(), options); - } else - bm = BitmapFactory.decodeFile(file.getAbsolutePath()); - } + int factor = 1; + while (options.outWidth / factor > scaleToPixels) + factor *= 2; - if (bm != null) { - Matrix rotation = getImageRotation(file); - if (rotation != null) { - Bitmap rotated = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), rotation, true); - bm.recycle(); - bm = rotated; + Matrix rotation = getImageRotation(file); + if (factor > 1 || rotation != null) { + Log.i("Decode image factor=" + factor); + options.inJustDecodeBounds = false; + options.inSampleSize = factor; + Bitmap scaled = BitmapFactory.decodeFile(file.getAbsolutePath(), options); + + if (scaled != null && rotation != null) { + Bitmap rotated = Bitmap.createBitmap(scaled, 0, 0, scaled.getWidth(), scaled.getHeight(), rotation, true); + scaled.recycle(); + scaled = rotated; } + + return scaled; } - return bm; + return BitmapFactory.decodeFile(file.getAbsolutePath()); } static Matrix getImageRotation(File file) { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0ae92a5fd0..db45a55b3d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1055,6 +1055,7 @@ Connecting to one or more accounts … Folder does not exist The originally received message will be included + Image could not be decoded Search on server is not available for this account Message too large to completely reformat Message too large to display completely