Search code examples
androidbitmapfactory

android how to load/save original size Bitmap with no OutOfMemory


I read many discussions about the inSampleSize OutOfMemory dilemma.
Cannot get a good solution so i ask a question about it.
Im currently loading a bitmap with inSampleSize=4.
That will give me a Bitmap with the size 648x388.
Original On disk size is 2592x1592.

Im writing text on 648x388 bitmap and saving it back to disk.
Im writing on the 648x388 because the 2592x1592 give me OutOfMemory .

The way it works is that there can be 1-10 648x388 Bitmaps to be saved in a while loop.
I want to change this loop to save 1-10 2592x1592 Bitmaps.

How can i securely load the 2592x1592?
I don care about the resolution going down 60% or more.
As long as the Bitmap has the same size 2592x1592.
Is there a way to maybe keep the size but make Bitmap thinner,
removing color without making quality bad.

My first thought was going something like this to get the biggest bitmap i could get: I have not tested this but get a feeling it's a bad way

boolean work = true;
int insample = 2;
BitmapFactory.Options options = new BitmapFactory.Options();
while(work){
    try{
    options.inSampleSize = insample;
        bitmap = BitmapFactory.decodeFile(filePath,options);
        work = false;
    }catch(Exception e){
        insample++;
    }
}

any help would be grate.


Solution

  • Image processing requires a lot of memory. you cant use the whole bitmap and just modify it on your phone. use a web service for that. upload, process, download. sorry there is no other way a decoded bitmap just takes a lot of memory.

    And by the way you cant catch an outOFMemory Exception. the app just crashes.