0

I'm tring to get image from gallery (by intent).
I got this error:

985120-byte external allocation too large for this process.
Out of memory: Heap Size=4871KB, Allocated=2472KB, Bitmap Size=19677KB
VM won't let us allocate 985120 bytes

That's my code where I get image:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   ....
   mBitmap = Media.getBitmap(this.getContentResolver(), data.getData());
   ...
}

How can i solve it ?

-------- UPDATE ---------

I noticed that if I select a pre-existent image (HTC photo installed) I get this error. If I select image picked from camera all works fine.

So, I change my code according to this http://developer.android.com/training/displaying-bitmaps/load-bitmap.html:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

InputStream stream = getContentResolver().openInputStream(data.getData());
mBitmap = BitmapFactory.decodeStream(stream,null,options);
stream.close();

But now the bitmap is NULL !!!

enfix
  • 6,680
  • 12
  • 55
  • 80
  • Possible duplicate of http://stackoverflow.com/questions/6118464/android-out-of-memory-error-bitmap-is-too-big?rq=1 – Ridcully Aug 27 '12 at 18:38
  • options.inJustDecodeBounds = true; with this you only decode the size of the bitmap, not allocate it, so that's why it is null. Try using options.inScale to decrease the size of the bitmap. – Marcio Covre Aug 27 '12 at 19:01
  • http://stackoverflow.com/a/4665992/1615280 – bjorncs Aug 27 '12 at 19:17
  • http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – esse Dec 02 '12 at 20:02

2 Answers2

1

It looks like your application uses a lot of high-res bitmap (the bitmap memory partition is 19677KB). The sie of 'heap' and 'allocated' are quite normal, and should not be of any problem. Make sure that you remove unused bitmap from memory. You can free a bitmap from memory by calling bitmap.recycle(), or setting the reference to it to null. Take a look at LruCache if you want to cache bitmaps for performance reasons.

bjorncs
  • 1,250
  • 11
  • 20
  • [LruCache](http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html) example with a bit more details – zapl Aug 27 '12 at 18:46
  • I got this error when i pick example image in my gallery (htc image installed) that is 19MB (maybe). – enfix Aug 27 '12 at 19:00
  • Okay, I thought that the 985120 bytes mentioned in the error message was the size of your image. 19MB is a lot of data, there may not be any good solution for that if you are using a low-spec HTC phone. There is a function for resizing images on the fly while parsing, hang on.... – bjorncs Aug 27 '12 at 19:07
  • This work if I add stream.close() before second usage. Otherwise I got bitmap=null – enfix Aug 27 '12 at 21:24
0

I always wrap decoding in a while loop increasing the inSampleSize and catching OutOfMemoryError. This will give you the maximum possible resolution image. Always use LRU caches!

    Bitmap image;
    boolean success = false;int counter = 0;
    while (success == false && counter < 10)
    {
        try
        {
            image = BitmapFactory.decodeFile(photoPath, options);
            success = true;
        }
        catch(OutOfMemoryError e)
        {
            System.gc();
            options.inSampleSize++;
            counter++;
        }
    }
Noah Seidman
  • 4,359
  • 5
  • 26
  • 28