-1

I'm running into issues with the images in my gallery being much much larger than I need them to be.

I've looked for ways to reduce their size before I actually pull them in, but I'm just not quite putting it together as most of what I'm finding deals with BitMap resources and not a BitMap that already exists in the gallery.

So, basically, I am getting the image like so,

  imageBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

But before I actually assign it to my BitMap var over there I need to scale it down to a reasonable size for a phone.

Any help with understanding what is going on here better is appreciated.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
BrandenS
  • 591
  • 1
  • 5
  • 7

1 Answers1

0

getBitmap() is a weak convenience method. Its body is a whopping four lines of code:

public static final Bitmap getBitmap(ContentResolver cr, Uri url)
        throws FileNotFoundException, IOException {
    InputStream input = cr.openInputStream(url);
    Bitmap bitmap = BitmapFactory.decodeStream(input);
    input.close();
    return bitmap;
}

This is why I don't bother teaching people about it.

IMHO, the best solution is for you to use one of the many image-loading libraries available for Android, such as Picasso. Most of the good ones can load from a Uri and handle your resizing as part of the operation, doing the heavy lifting on a background thread.

If, for whatever reason, you want to do all that work yourself, call BitmapFactory.decodeStream() with a BitmapFactory.Options object. In particular, set inSampleSize to indicate that you want the image to be resampled as part of reading it, so you wind up with a smaller Bitmap taking up less heap space.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Oh man....this is exactly the answer I was looking to get because it made me realize that what I was doing didn't even really need to work with Bitmaps at all, I can just directly use the Uri. That was the little piece that I was missing and it came about because I snatched someones code that used the getBitmap method without understanding what it was doing. Thanks! – BrandenS Aug 18 '16 at 23:41