Created
February 20, 2014 23:18
-
-
Save lexer/9125400 to your computer and use it in GitHub Desktop.
Reduce image size when picasso fail with OutOfMemmory
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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