From 6ed3931a62b46f7e52565afeee6e882acdbe1c62 Mon Sep 17 00:00:00 2001 From: Philip Flohr Date: Fri, 13 Nov 2015 23:43:00 +0100 Subject: scale the image to avoid OOM Execptions --- .../ui/PublishProfilePictureActivity.java | 37 ++++++++++++++++++++-- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java index e48e6a1cb..af2711dae 100644 --- a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java @@ -159,7 +159,7 @@ public class PublishProfilePictureActivity extends XmppActivity { if (requestCode == REQUEST_CHOOSE_FILE) { this.avatarUri = data.getData(); Uri destination = Uri.fromFile(new File(getCacheDir(), "croppedAvatar")); - Crop.of(this.avatarUri, destination).withMaxSize(Config.AVATAR_SIZE, Config.AVATAR_SIZE).asSquare().start(PublishProfilePictureActivity.this); + Crop.of(this.avatarUri, destination).asSquare().start(PublishProfilePictureActivity.this); } } if (requestCode == Crop.REQUEST_CROP) { @@ -231,11 +231,42 @@ public class PublishProfilePictureActivity extends XmppActivity { } } + private int calculateInSampleSize( + BitmapFactory.Options options, int reqWidth, int reqHeight) { + // Raw height and width of image + final int height = options.outHeight; + final int width = options.outWidth; + int inSampleSize = 1; + + if (height > reqHeight || width > reqWidth) { + + final int halfHeight = height / 2; + final int halfWidth = width / 2; + + // Calculate the largest inSampleSize value that is a power of 2 and keeps both + // height and width larger than the requested height and width. + while ((halfHeight / inSampleSize) > reqHeight + && (halfWidth / inSampleSize) > reqWidth) { + inSampleSize *= 2; + } + } + + return inSampleSize; + } + + private Bitmap loadScaledBitmap(String filePath, int reqWidth, int reqHeight) { + final BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(filePath,options); + options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); + options.inJustDecodeBounds = false; + return BitmapFactory.decodeFile(filePath,options); + } protected void loadImageIntoPreview(Uri uri) { Bitmap bm = null; try{ - bm = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri); - } catch (IOException e) { + bm = loadScaledBitmap(uri.getPath(), Config.AVATAR_SIZE, Config.AVATAR_SIZE); + } catch (Exception e) { e.printStackTrace(); } -- cgit v1.2.3