From 99e3e596bbfe337fef22c7c1033b36f9caa03500 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Mon, 30 Sep 2013 23:56:50 -0500 Subject: [PATCH] Correctly preserve MMS image aspect ratios. --- .../securesms/util/BitmapUtil.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/org/thoughtcrime/securesms/util/BitmapUtil.java b/src/org/thoughtcrime/securesms/util/BitmapUtil.java index ea60bbc197..0d786da20e 100644 --- a/src/org/thoughtcrime/securesms/util/BitmapUtil.java +++ b/src/org/thoughtcrime/securesms/util/BitmapUtil.java @@ -67,9 +67,22 @@ public class BitmapUtil { throw new BitmapDecodingException("Decoded stream was null."); } - if (imageWidth > maxWidth || imageHeight > maxHeight) { - Log.w("BitmapUtil", "Scaling to max width and height: " + maxWidth + "," + maxHeight); - Bitmap scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, maxWidth, maxHeight, true); + if (roughThumbnail.getWidth() > maxWidth || roughThumbnail.getHeight() > maxHeight) { + float aspectWidth, aspectHeight; + + if (imageWidth == 0 || imageHeight == 0) { + aspectWidth = maxWidth; + aspectHeight = maxHeight; + } else if (options.outWidth >= options.outHeight) { + aspectWidth = maxWidth; + aspectHeight = (aspectWidth / options.outWidth) * options.outHeight; + } else { + aspectHeight = maxHeight; + aspectWidth = (aspectHeight / options.outHeight) * options.outWidth; + } + + Log.w("BitmapUtil", "Scaling to max width and height: " + aspectWidth + "," + aspectHeight); + Bitmap scaledThumbnail = Bitmap.createScaledBitmap(roughThumbnail, (int)aspectWidth, (int)aspectHeight, true); roughThumbnail.recycle(); return scaledThumbnail; } else {