From b9b500f45714a2856bdc28cccee42e3a184137ee Mon Sep 17 00:00:00 2001 From: Konrad Pozniak Date: Mon, 18 Jun 2018 13:26:31 +0200 Subject: [PATCH] fix media resizing (#686) * fix media resizing * move exception catching out of method --- .../com/keylesspalace/tusky/ComposeActivity.java | 16 +++++++++++----- .../tusky/util/DownsizeImageTask.java | 6 ++---- .../com/keylesspalace/tusky/util/MediaUtils.java | 13 +++++++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java index 920632e6..81e0abac 100644 --- a/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java +++ b/app/src/main/java/com/keylesspalace/tusky/ComposeActivity.java @@ -85,7 +85,6 @@ import com.keylesspalace.tusky.adapter.EmojiAdapter; import com.keylesspalace.tusky.adapter.MentionAutoCompleteAdapter; import com.keylesspalace.tusky.adapter.OnEmojiSelectedListener; import com.keylesspalace.tusky.db.AccountEntity; -import com.keylesspalace.tusky.db.AccountManager; import com.keylesspalace.tusky.db.AppDatabase; import com.keylesspalace.tusky.db.InstanceEntity; import com.keylesspalace.tusky.di.Injectable; @@ -151,6 +150,7 @@ public final class ComposeActivity private static final String TAG = "ComposeActivity"; // logging tag static final int STATUS_CHARACTER_LIMIT = 500; private static final int STATUS_MEDIA_SIZE_LIMIT = 8388608; // 8MiB + private static final int STATUS_MEDIA_PIXEL_SIZE_LIMIT = 16777216; // 4096^2 Pixels private static final int MEDIA_PICK_RESULT = 1; private static final int MEDIA_TAKE_PHOTO_RESULT = 2; private static final int PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 1; @@ -1063,10 +1063,16 @@ public final class ComposeActivity if (item.readyStage != QueuedMedia.ReadyStage.UPLOADED) { waitForMediaLatch.countUp(); - if (mediaSize > STATUS_MEDIA_SIZE_LIMIT && type == QueuedMedia.Type.IMAGE) { - downsizeMedia(item); - } else { - uploadMedia(item); + + try { + if (type == QueuedMedia.Type.IMAGE && + (mediaSize > STATUS_MEDIA_SIZE_LIMIT || MediaUtils.getImageSquarePixels(getContentResolver(), item.uri) > STATUS_MEDIA_PIXEL_SIZE_LIMIT)) { + downsizeMedia(item); + } else { + uploadMedia(item); + } + } catch (FileNotFoundException e) { + onUploadFailure(item, false); } } } diff --git a/app/src/main/java/com/keylesspalace/tusky/util/DownsizeImageTask.java b/app/src/main/java/com/keylesspalace/tusky/util/DownsizeImageTask.java index c9df6a19..4df25889 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/DownsizeImageTask.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/DownsizeImageTask.java @@ -172,7 +172,6 @@ public class DownsizeImageTask extends AsyncTask { * test, and keep trying at smaller sizes. The initial estimate should be good for * many cases, so it should only iterate once, but the loop is used to be absolutely * sure it gets downsized to below the limit. */ - int iterations = 0; int scaledImageSize = 1024; do { stream.reset(); @@ -208,12 +207,11 @@ public class DownsizeImageTask extends AsyncTask { } else { format = Bitmap.CompressFormat.PNG; } - reorientedBitmap.compress(format, 75, stream); + reorientedBitmap.compress(format, 85, stream); reorientedBitmap.recycle(); scaledImageSize /= 2; - iterations++; } while (stream.size() > sizeLimit); - Assert.expect(iterations < 3); + resultList.add(stream.toByteArray()); if (isCancelled()) { return false; diff --git a/app/src/main/java/com/keylesspalace/tusky/util/MediaUtils.java b/app/src/main/java/com/keylesspalace/tusky/util/MediaUtils.java index bcce0da7..3341f6d3 100644 --- a/app/src/main/java/com/keylesspalace/tusky/util/MediaUtils.java +++ b/app/src/main/java/com/keylesspalace/tusky/util/MediaUtils.java @@ -126,4 +126,17 @@ public class MediaUtils { source.recycle(); return bitmap; } + + public static long getImageSquarePixels(ContentResolver contentResolver, Uri uri) throws FileNotFoundException { + InputStream input; + input = contentResolver.openInputStream(uri); + + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeStream(input, null, options); + + IOUtils.closeQuietly(input); + + return (long) options.outWidth * options.outHeight; + } }