BitmapFactory decodeStream into file. Or how to deal with bitmaps without killing VM?
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.decodeStrea开发者_开发技巧m(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?
Try calling Bitmap.recycle after you've finished with one Bitmap but before you load the next.
精彩评论