Skip to content

Instantly share code, notes, and snippets.

@lexer
Created February 20, 2014 23:18
Show Gist options
  • Save lexer/9125400 to your computer and use it in GitHub Desktop.
Save lexer/9125400 to your computer and use it in GitHub Desktop.
Reduce image size when picasso fail with OutOfMemmory
private Bitmap safeLoadBitmap(File file, int width, int height) throws IOException {
RequestCreator picassoRequestBuilder = picasso.load(file)
.resize(width, height)
.centerInside();
if (getTransformation() != null) {
picassoRequestBuilder.transform(getTransformation());
}
Bitmap bitmap = null;
try {
bitmap = picassoRequestBuilder.get();
} catch (OutOfMemoryError error) {
if (width < MIN_WIDTH || height < MIN_HEIGHT) {
throw error;
}
return safeLoadBitmap(file, width / 2, height / 2);
}
return bitmap;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment