Skip to content
This repository was archived by the owner on May 13, 2025. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/com/yelp/android/webimageview/WebImageView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, but I think it's better to rethrow the OutOfMemoryError after it fails for the second time. Swallowing that Error will probably make the JVM unstable afterwards.

}
return;
} else if (url.startsWith("bundle://")) {
url = url.substring("bundle://".length());
Expand All @@ -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();
Expand Down