Android out of memory on image capture

前端 未结 2 1382
春和景丽
春和景丽 2020-12-31 20:01

I have an Activity which takes photos (with full possible resolution, so quite large), the application have then the chance to analyze them. Only one photo is handled at a t

2条回答
  •  既然无缘
    2020-12-31 20:41

    This may not be exactly what you are trying to do, but in order to display multiple large pictures (4-6 MB) on a grid view, I found this code to create a smaller bitmap with lower quality, to avoid out-of-memory situations:

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 5;
    options.inPurgeable = true;
    options.inInputShareable = true;
    Bitmap bm = BitmapFactory.decodeFile("/sdcard/myapp/" + filesFound.get(position), options);
    

    The options are the important part, and by varying the settings, I managed to take memory down from where it would crash, to around 28MB when I started using the inSampleSize. It further went down with the inPurgeable and inInputShareable settings set to true. My activity is still sluggish, but it's much better.

    For your application, if it can analyze a bitmap, the above code may shrink down the memory usage enough to make it work. I'm still new to Android, so it's possible this may not work at all.. ;-).

    Regards,

    Kevin

提交回复
热议问题