Handle failed bitmap decoding

This commit is contained in:
Moxie Marlinspike
2013-05-21 10:32:48 -07:00
parent 3df67a1643
commit a0a6c3f211
5 changed files with 30 additions and 5 deletions

View File

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

View File

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