1

I'm struggling with OOM errors when decoding images. This message is right on money what I'm dealing with: BitmapFactory OOM driving me nuts

I think I KNOW what might solve my issue but not sure how to do it. Right now when I'm resizing image - I'm getting Bitmap like this:

//Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            o2.inDither=false;          //Disable Dithering mode
            o2.inPurgeable=true;        //Tell to gc that whether it needs free memory, the Bitmap can be cleared
            o2.inInputShareable=true;   //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
            o2.inTempStorage=new byte[32 * 1024];
            Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file), null, o2);

And than I need to save it back to file:

FileOutputStream out = new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);

As you see - my goal is to get reduced image in storage.

Right now - when I operate big Bitmaps - I get big file loaded into memory which pushes heap size UP and then on next try - I get OOM exception because there is not enough memory in Native heap.

So, I'd like to get stream from BitmapFactory and somehow pass it into FileStream for storage. That would eliminate VM heap issue if I want big images.

Anybody know how?

Community
  • 1
  • 1
katit
  • 17,375
  • 35
  • 128
  • 256

1 Answers1

0

Try calling Bitmap.recycle after you've finished with one Bitmap but before you load the next.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • That doesn't help. I'm very careful with this stuff. Issue with creating Bitmap object.. I need some kind of stream-parser – katit Jul 11 '11 at 03:16
  • That doesn't help? As in, you are calling `recycle` and it does not improve the memory profile? – Femi Jul 11 '11 at 03:19
  • I never see VM Heap going down. So, for example I create Bitmap - it pushes VM heap UP and then I recycle bitmap. I do get memory back but VM heap still allocated to my app. – katit Jul 11 '11 at 03:21
  • For example, VM heap = 5mb and actual used memory = 3mb. Than I load large bitmap - heap goes up to 9mb and after I recycle - memory goes back to 3mb but heap stays 9mb – katit Jul 11 '11 at 03:23
  • Ah, well, you really don't have control over the heap size from application code. As far as I know there is no existing framework interface to do what you want: to resize the Bitmap you will need to load the entire Bitmap into memory, as I can't see how trying to resize a partially loaded image could work without astonishing effort (resizes usually require some form of sampling or interpolation which generally require multiple points to be loaded). If the `recycle` call lets you continue to load more images without the OOM that should work, but let us know if you find anything else. – Femi Jul 11 '11 at 03:29
  • Honestly, I'm giving up here. It's weird. I guess it depends on other apps running as well. Sometime heap size will be 10Mb and I will get OOM and sometime it will be 20Mb and I won't get it. When I scale image to 800x600 it works good but if I try to go higher - it happens. For my purpose I decided to allow 800x600 and then "Original" in options because it seems like I can easily upload megabytes but I can't downsample same... – katit Jul 11 '11 at 03:34