I'm working in a project that opens an image, plays with it, and saves. The image can be a very huge image, like a camera pic, so I'm doing several things to ensure not getting OutOfMemory exceptions (basically what appears here http://developer.android.com/training/displaying-bitmaps/load-bitmap.html):
- Perform all measurements of width and height of the image without actually open it, using:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image_path, options);
Open images already resized ( inSampleSize ) and using
Bitmap.Config.RGB_565
(2B per pixel) besidesBitmap.Config.ARGB_8888
(4B per pixel).Using temp files to not having a lot of bitmaps in memory (actually I only have one most of the cases, max 2 when applying masks or so).
Recycling every bitmap after its use
When showing the result in a
ImageView
, open it already scaled to the screen sizes.
But, in some cases, when it's impossible to have only one bitmap loaded at a time to do a certain work (rotating the image, fine tune resizing, apply masks, ...), and mostly in medium range or old devices, I get an OutOfMemory Exception.
I've started to add all the Image working in Background Jobs with something like the following (ok, I'm a newbie):
ProgressDialog dialog = ProgressDialog.show(this, null, "Please wait", true, false);
new Thread(new BackgroundJob(this, new Runnable() {
public void run() {
ImageHelper.ImageManipulation(image_path);
}
}, dialog, new Handler())).start();
I was hoping to get a completely memory free space with this new thread to perform there the Image manipulation tasks, but what I find is that the app crashes at same points for same reasons. Threads are created as a child process (ok) but sharing the max heap and the current used mem ...
Is there a way to open a thread to perform image manipulation having more memory to work (freeing the memory used by the activity itself as it is another thread, for example)?
Thank you very much.