From 761b51b493fe1234118ecc2c7cded64b3dea7251 Mon Sep 17 00:00:00 2001 From: Michael Stephens Date: Thu, 3 Oct 2013 19:15:50 -0700 Subject: [PATCH] Added OutOfMemory catch and retry for loading images from the file system --- .../android/webimageview/WebImageView.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/com/yelp/android/webimageview/WebImageView.java b/src/com/yelp/android/webimageview/WebImageView.java index f29103c..5b1d7d1 100644 --- a/src/com/yelp/android/webimageview/WebImageView.java +++ b/src/com/yelp/android/webimageview/WebImageView.java @@ -163,8 +163,14 @@ public void setImageUrl(String url, boolean load, ImageLoadedCallback callback, if (reqWidth > 0 && reqHeight > 0) { options.inSampleSize = calculateInSampleSize(filename, reqWidth, reqHeight); } - Bitmap bitmap = BitmapFactory.decodeFile(filename, options); - setImageBitmap(bitmap); + if (!setImageFromFile(filename, options)) { + // Sub-sample once, try again. If that fails, just give up. + if (options.inSampleSize == 0) { + options.inSampleSize = 1; + } + options.inSampleSize <<= 1; + setImageFromFile(filename, options); + } return; } else if (url.startsWith("bundle://")) { url = url.substring("bundle://".length()); @@ -175,6 +181,16 @@ public void setImageUrl(String url, boolean load, ImageLoadedCallback callback, } } } + + private boolean setImageFromFile(String filename, BitmapFactory.Options options) { + try { + Bitmap bitmap = BitmapFactory.decodeFile(filename, options); + setImageBitmap(bitmap); + } catch (OutOfMemoryError e) { + return false; + } + return true; + } private static int calculateInSampleSize(String filename, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options();