From 6a94e8a5f99d12830140e8ead5f1757b1ec20e0d Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 30 Oct 2019 13:11:52 +0100 Subject: [PATCH] Buffer downloaded image data --- .../java/eu/faircode/email/ImageHelper.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/ImageHelper.java b/app/src/main/java/eu/faircode/email/ImageHelper.java index 912b159f49..455599d441 100644 --- a/app/src/main/java/eu/faircode/email/ImageHelper.java +++ b/app/src/main/java/eu/faircode/email/ImageHelper.java @@ -50,12 +50,12 @@ import androidx.core.graphics.ColorUtils; import androidx.exifinterface.media.ExifInterface; import androidx.preference.PreferenceManager; +import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; -import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import java.security.MessageDigest; @@ -345,23 +345,22 @@ class ImageHelper { return; } - BitmapFactory.Options options = new BitmapFactory.Options(); - Log.i("Probe " + a.source); - try (InputStream probe = new URL(a.source).openStream()) { + Bitmap bm; + try (BufferedInputStream is = new BufferedInputStream(new URL(a.source).openStream())) { + Log.i("Probe " + a.source); + is.mark(8192); + BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; - BitmapFactory.decodeStream(probe, null, options); - } + BitmapFactory.decodeStream(is, null, options); - Log.i("Download " + a.source); - Bitmap bm; - try (InputStream is = new URL(a.source).openStream()) { int scaleTo = res.getDisplayMetrics().widthPixels; int factor = 1; while (options.outWidth / factor > scaleTo) factor *= 2; + Log.i("Download " + a.source + " factor=" + factor); + is.reset(); if (factor > 1) { - Log.i("Download image factor=" + factor); options.inJustDecodeBounds = false; options.inSampleSize = factor; bm = BitmapFactory.decodeStream(is, null, options);