0

I just added support for album art in my Android application and I've encountered a problem where displaying the album art in a layout causes the application memory to spike and the playback service is eventually killed to free up memory. I believe the issue is that I'm adding the extracted album art to the layout without compressing it. This results in a large image having to be cached in memory. The code I'm using to make the Bitmap is:

byte [] blob = mCursor.getBlob(mCursor.getColumnIndexOrThrow(Media.MediaColumns.PICTURE));

if (blob != null) {
    return BitmapFactory.decodeByteArray(blob, 0, blob.length);
}

Is it possible to uniformly scale/compress these Bitmaps to reduce their memory footprint. Also, is there a way to compress directly using the byte array (rather then an inputstream).

William Seemann
  • 3,440
  • 10
  • 44
  • 78

1 Answers1

3

Try this

Options opt = new Options();
        opt.inSampleSize = 2;
        if (blob != null) {
        return  BitmapFactory.decodeByteArray(blob, 0, length, opt)
          }

More info about this

public int inSampleSize
Added in API level 1

If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. The sample size is the number of pixels in either dimension that correspond to a single pixel in the decoded bitmap. For example, inSampleSize == 4 returns an image that is 1/4 the width/height of the original, and 1/16 the number of pixels. Any value <= 1 is treated the same as 1. Note: the decoder will try to fulfill this request, but the resulting bitmap may have different dimensions that precisely what has been requested. Also, powers of 2 are often faster/easier for the decoder to honor.

Ali Imran
  • 8,927
  • 3
  • 39
  • 50
  • More on this :-http://stackoverflow.com/questions/3331527/android-resize-a-large-bitmap-file-to-scaled-output-file – Ali Imran Dec 08 '12 at 18:01