1

I need to let the user open a specific album from their gallery, and let them to do something with images.

In order to retrieve an image from an album, I'm using:
Bitmap bitmap = MediaStore.Images.Media.getBitmap(contentResolver, uri).

Everything works fine, except the fact that if the album contains many pictures, it ends up trowing an OutOfMemoryException.
Now, I know how to mitigate this issue based on Android guidelines, but the problem is that I'm already retrieving the original Bitmap with getBitmap()

So, is there a possibility to retrieve the image in the byte array format, or an input stream format, and scale it down before assigning it in memory to avoid memory leaks? (in the same way the Android guidelines advice)

2 Answers 2

0

You've already identified a very good solution. If you want to skip the step of pulling the image into a Bitmap via the MediaStore, try using ImageView.setImageUri().

1
  • Hi, thank you for the comment. Trying to pass the Uri to the ImageView before scaling also is causing OutOfMemoryException it the image is too big. But you opened up a new alternative for me, and based on that image Uri now I'm able to retrieve the InputStream and scale down the image. I'll post the solution soon.
    – Andy Res
    Jul 27, 2012 at 11:36
0

So, having a image Uri in my hands I wanted to retrieve it's InputStream and scale down the image before assigning it in memory to avoid OutOfMemoryException

Solution:
To retrieve the InputStream from an Uri, you have to call this:

InputStream stream = getContentResolver().openInputStream(uri);

Then following Android recommendations on loading bitmaps efficiently, you just need to call BitmapFactory.decodeStream(), and pass the BitmapFactory.Options as a parameter.

Full source code:

imageView = (ImageView) findViewById(R.id.imageView);

Uri uri = Uri.parse("android.resource://com.testcontentproviders/drawable/"+R.drawable.test_image_large);
Bitmap bitmap=null;
    try {
        InputStream stream = getContentResolver().openInputStream(uri);
        bitmap=decodeSampledBitmapFromStream(stream, 150, 100);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

imageView.setImageBitmap(bitmap);

Helper methods:

public static Bitmap decodeSampledBitmapFromStream(InputStream stream,
            int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(stream, null, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeStream(stream, null, options);
}

public static 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) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.