From df8ce2e84db0b4d779b34eb340b4fcc8494b074d Mon Sep 17 00:00:00 2001 From: Danilo Ercoli Date: Wed, 10 Sep 2014 15:46:48 +0200 Subject: [PATCH] Catch NPE in Bitmap.CreateBitmap and make sure the returned bitmap is not null --- .../org/wordpress/android/util/ImageUtils.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java b/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java index 31dadc911124..10c6fb478605 100644 --- a/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java +++ b/WordPressUtils/src/main/java/org/wordpress/android/util/ImageUtils.java @@ -444,7 +444,7 @@ public static byte[] createThumbnailFromUri(Context context, optActual.inSampleSize = scale; // Get the roughly resized bitmap - Bitmap bmpResized; + final Bitmap bmpResized; try { bmpResized = BitmapFactory.decodeFile(filePath, optActual); } catch (OutOfMemoryError e) { @@ -452,8 +452,9 @@ public static byte[] createThumbnailFromUri(Context context, return null; } - if (bmpResized == null) + if (bmpResized == null) { return null; + } ByteArrayOutputStream stream = new ByteArrayOutputStream(); @@ -490,7 +491,18 @@ public static byte[] createThumbnailFromUri(Context context, } catch (OutOfMemoryError e) { AppLog.e(AppLog.T.UTILS, "OutOfMemoryError Error in setting image: " + e); return null; + } catch (NullPointerException e) { + // See: https://github.com/wordpress-mobile/WordPress-Android/issues/1844 + AppLog.e(AppLog.T.UTILS, "Bitmap.createBitmap has thrown a NPE internally. This should never happen: " + e); + return null; + } + + if (bmpRotated == null) { + // Fix an issue where bmpRotated is null even if the documentation doesn't say Bitmap.createBitmap can return null. + // See: https://github.com/wordpress-mobile/WordPress-Android/issues/1848 + return null; } + bmpRotated.compress(fmt, 100, stream); bmpResized.recycle(); bmpRotated.recycle();