Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's the correct way of doing it, keeping tabs on memory usage as well:</p> <pre><code>protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Uri imageUri = data.getData(); Bitmap bitmap = getThumbnail(imageUri); } } public static Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException{ InputStream input = this.getContentResolver().openInputStream(uri); BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options(); onlyBoundsOptions.inJustDecodeBounds = true; onlyBoundsOptions.inDither=true;//optional onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional BitmapFactory.decodeStream(input, null, onlyBoundsOptions); input.close(); if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1)) { return null; } int originalSize = (onlyBoundsOptions.outHeight &gt; onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth; double ratio = (originalSize &gt; THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0; BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio); bitmapOptions.inDither = true; //optional bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;// input = this.getContentResolver().openInputStream(uri); Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions); input.close(); return bitmap; } private static int getPowerOfTwoForSampleRatio(double ratio){ int k = Integer.highestOneBit((int)Math.floor(ratio)); if(k==0) return 1; else return k; } </code></pre> <p>The getBitmap() call from Mark Ingram's post also calls the decodeStream(), so you don't lose any functionality.</p> <p>References:</p> <ul> <li><p><a href="https://stackoverflow.com/questions/4916159/android-get-thumbnail-of-image-on-sd-card-given-uri-of-original-image">Android: Get thumbnail of image on SD card, given Uri of original image</a></p></li> <li><p><a href="https://stackoverflow.com/questions/2220949/handling-large-bitmaps">Handling large Bitmaps</a></p></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload