Skip to content

Instantly share code, notes, and snippets.

@voituk
Created April 11, 2012 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save voituk/2359091 to your computer and use it in GitHub Desktop.
Save voituk/2359091 to your computer and use it in GitHub Desktop.
Android: Load + resize image
try {
BitmapFactory.Options options = new BitmapFactory.Options() {{
inJustDecodeBounds = true;
}};
BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, options);
final int wResample = (options.outWidth > profileThumb.getWidth()) ? options.outWidth / profileThumb.getWidth() : 1;
final int hResample = (options.outHeight > profileThumb.getHeight()) ? options.outHeight / profileThumb.getHeight() : 1;
Bitmap tinyBmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri), null, new BitmapFactory.Options() {{
inSampleSize = Math.max(1, Math.min(Math.max(wResample, hResample), 16));
inPreferredConfig = Bitmap.Config.RGB_565;
inPurgeable = true;
inInputShareable = true;
}});
profileThumb.setImageBitmap(tinyBmp);// ImageURI(imageFromGallery); // Let's allow android to handle decoding and etc
} catch (FileNotFoundException e) {
// this shouldn't happen. at least not very often
Looplr.reportException(e, "Error Loading profile picture");
Log.e(TAG, "Error Loading profile picture");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment