Skip to content

Instantly share code, notes, and snippets.

@blaswan
Created February 25, 2014 10:46
Show Gist options
  • Save blaswan/9206716 to your computer and use it in GitHub Desktop.
Save blaswan/9206716 to your computer and use it in GitHub Desktop.
public static Bitmap getScaledBitmapFromUri(Context context, Uri uri) {
InputStream inputStream;
try {
inputStream = context.getContentResolver().openInputStream(uri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
int width = options.outWidth;
int scaleFactor = width / SCALED_WIDTH;
Bitmap bitmap;
inputStream = context.getContentResolver().openInputStream(uri);
options = new BitmapFactory.Options();
options.inSampleSize = scaleFactor;
bitmap = BitmapFactory.decodeStream(inputStream, null, options);
inputStream.close();
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment