diff --git a/src/org/thoughtcrime/securesms/ConversationActivity.java b/src/org/thoughtcrime/securesms/ConversationActivity.java index 9e6b8e66e7..95cd2a3880 100644 --- a/src/org/thoughtcrime/securesms/ConversationActivity.java +++ b/src/org/thoughtcrime/securesms/ConversationActivity.java @@ -73,6 +73,7 @@ import org.thoughtcrime.securesms.service.KeyCachingService; import org.thoughtcrime.securesms.sms.MessageSender; import org.thoughtcrime.securesms.sms.OutgoingEncryptedMessage; import org.thoughtcrime.securesms.sms.OutgoingTextMessage; +import org.thoughtcrime.securesms.util.BitmapDecodingException; import org.thoughtcrime.securesms.util.CharacterCalculator; import org.thoughtcrime.securesms.util.EncryptedCharacterCalculator; import org.thoughtcrime.securesms.util.InvalidMessageException; @@ -619,10 +620,15 @@ public class ConversationActivity extends PassphraseRequiredSherlockFragmentActi try { attachmentManager.setImage(imageUri); } catch (IOException e) { + Log.w("ConversationActivity", e); + attachmentManager.clear(); + Toast.makeText(this, R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment, + Toast.LENGTH_LONG).show(); + } catch (BitmapDecodingException e) { + Log.w("ConversationActivity", e); attachmentManager.clear(); Toast.makeText(this, R.string.ConversationActivity_sorry_there_was_an_error_setting_your_attachment, Toast.LENGTH_LONG).show(); - Log.w("ComposeMessageActivity", e); } } diff --git a/src/org/thoughtcrime/securesms/mms/AttachmentManager.java b/src/org/thoughtcrime/securesms/mms/AttachmentManager.java index 8d9ff54c95..451391b06d 100644 --- a/src/org/thoughtcrime/securesms/mms/AttachmentManager.java +++ b/src/org/thoughtcrime/securesms/mms/AttachmentManager.java @@ -25,6 +25,7 @@ import android.widget.Button; import android.widget.ImageView; import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.util.BitmapDecodingException; import java.io.IOException; @@ -51,7 +52,7 @@ public class AttachmentManager { attachmentView.setVisibility(View.GONE); } - public void setImage(Uri image) throws IOException { + public void setImage(Uri image) throws IOException, BitmapDecodingException { ImageSlide slide = new ImageSlide(context, image); slideDeck.addSlide(slide); thumbnail.setImageBitmap(slide.getThumbnail(345, 261)); diff --git a/src/org/thoughtcrime/securesms/mms/ImageSlide.java b/src/org/thoughtcrime/securesms/mms/ImageSlide.java index d134d2507d..09089e60d2 100644 --- a/src/org/thoughtcrime/securesms/mms/ImageSlide.java +++ b/src/org/thoughtcrime/securesms/mms/ImageSlide.java @@ -32,6 +32,7 @@ import android.widget.ImageView; import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.crypto.MasterSecret; import org.thoughtcrime.securesms.database.MmsDatabase; +import org.thoughtcrime.securesms.util.BitmapDecodingException; import org.thoughtcrime.securesms.util.BitmapUtil; import org.thoughtcrime.securesms.util.LRUCache; @@ -56,7 +57,7 @@ public class ImageSlide extends Slide { super(context, masterSecret, part); } - public ImageSlide(Context context, Uri uri) throws IOException { + public ImageSlide(Context context, Uri uri) throws IOException, BitmapDecodingException { super(context, constructPartFromUri(context, uri)); } @@ -78,6 +79,9 @@ public class ImageSlide extends Slide { } catch (FileNotFoundException e) { Log.w("ImageSlide", e); return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_missing_thumbnail_picture); + } catch (BitmapDecodingException e) { + Log.w("ImageSlide", e); + return BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_missing_thumbnail_picture); } } @@ -148,7 +152,9 @@ public class ImageSlide extends Slide { return true; } - private static PduPart constructPartFromUri(Context context, Uri uri) throws IOException { + private static PduPart constructPartFromUri(Context context, Uri uri) + throws IOException, BitmapDecodingException + { PduPart part = new PduPart(); byte[] data = BitmapUtil.createScaledBytes(context, uri, 640, 480, (300 * 1024) - 5000); diff --git a/src/org/thoughtcrime/securesms/util/BitmapDecodingException.java b/src/org/thoughtcrime/securesms/util/BitmapDecodingException.java new file mode 100644 index 0000000000..304dbcb023 --- /dev/null +++ b/src/org/thoughtcrime/securesms/util/BitmapDecodingException.java @@ -0,0 +1,7 @@ +package org.thoughtcrime.securesms.util; + +public class BitmapDecodingException extends Throwable { + public BitmapDecodingException(String s) { + super(s); + } +} diff --git a/src/org/thoughtcrime/securesms/util/BitmapUtil.java b/src/org/thoughtcrime/securesms/util/BitmapUtil.java index a007dbe1ad..ea60bbc197 100644 --- a/src/org/thoughtcrime/securesms/util/BitmapUtil.java +++ b/src/org/thoughtcrime/securesms/util/BitmapUtil.java @@ -19,7 +19,7 @@ public class BitmapUtil { public static byte[] createScaledBytes(Context context, Uri uri, int maxWidth, int maxHeight, int maxSize) - throws IOException + throws IOException, BitmapDecodingException { InputStream measure = context.getContentResolver().openInputStream(uri); InputStream data = context.getContentResolver().openInputStream(uri); @@ -44,6 +44,7 @@ public class BitmapUtil { public static Bitmap createScaledBitmap(InputStream measure, InputStream data, int maxWidth, int maxHeight) + throws BitmapDecodingException { BitmapFactory.Options options = getImageDimensions(measure); int imageWidth = options.outWidth; @@ -62,6 +63,10 @@ public class BitmapUtil { Bitmap roughThumbnail = BitmapFactory.decodeStream(data, null, options); + if (roughThumbnail == null) { + 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);