Catch OutOfMemory exceptions on Android

This commit is contained in:
Florian Rival 2015-12-10 17:34:12 +01:00
parent 6f5ff49c68
commit 5d187494a3
1 changed files with 22 additions and 16 deletions

View File

@ -18,6 +18,7 @@ import java.util.Date;
class ImageResizer {
private static Bitmap resizeImage(String imagePath, int maxWidth, int maxHeight) {
try {
Bitmap image = BitmapFactory.decodeFile(imagePath);
if (maxHeight > 0 && maxWidth > 0) {
int width = image.getWidth();
@ -28,15 +29,20 @@ class ImageResizer {
int finalWidth = maxWidth;
int finalHeight = maxHeight;
if (ratioMax > 1) {
finalWidth = (int) ((float)maxHeight * ratioBitmap);
finalWidth = (int) ((float) maxHeight * ratioBitmap);
} else {
finalHeight = (int) ((float)maxWidth / ratioBitmap);
finalHeight = (int) ((float) maxWidth / ratioBitmap);
}
image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);
return image;
} else {
return image;
}
} catch (OutOfMemoryError ex) {
// No memory available for resizing.
}
return null;
}
public static Bitmap rotateImage(Bitmap b, float degrees)
@ -53,7 +59,7 @@ class ImageResizer {
b = b2;
}
} catch (OutOfMemoryError ex) {
// We have no memory to rotate. Return the original bitmap.
// No memory available for rotating. Return the original bitmap.
}
}
return b;