diff options
author | Christian S <kriztan@users.noreply.github.com> | 2015-11-14 10:13:38 +0100 |
---|---|---|
committer | Christian S <kriztan@users.noreply.github.com> | 2015-11-14 10:13:38 +0100 |
commit | 5f6be7578b5d3e0b1a230ef5154471b3eca2ce66 (patch) | |
tree | e964fc37a838a1a2bfbf99b740cb49066fcd58f2 /src | |
parent | 2fb08c58f30681d2ac840c0d00e939e6025f7042 (diff) | |
parent | 6ed3931a62b46f7e52565afeee6e882acdbe1c62 (diff) |
Merge pull request #51 from philipflohr/master
scale the image to avoid OOM Execptions
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java b/src/main/java/eu/siacs/conversations/ui/PublishProfilePictureActivity.java index e56a9279d..7a2d3f8dd 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(); } |