Handle failed bitmap decoding

pull/1/head
Moxie Marlinspike 11 years ago
parent 3df67a1643
commit a0a6c3f211

@ -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);
}
}

@ -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));

@ -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);

@ -0,0 +1,7 @@
package org.thoughtcrime.securesms.util;
public class BitmapDecodingException extends Throwable {
public BitmapDecodingException(String s) {
super(s);
}
}

@ -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);

Loading…
Cancel
Save