Resize a large bitmap file to scaled output file on Android

I have a large bitmap (say 3888×2592) in a file. Now, I want to resize that bitmap to 800×533 and save it to another file.
I normally would scale the bitmap by calling Bitmap.createBitmap method but it needs a source bitmap as the first argument, which I can’t provide because loading the original image into a Bitmap object would of course exceed the memory (see here, for example).

I also can’t read the bitmap with, for example, BitmapFactory.decodeFile(file, options), providing a BitmapFactory.Options.inSampleSize, because I want to resize it to an exact width and height. Using inSampleSize would resize the bitmap to 972×648 (if I use inSampleSize=4) or to 778×518 (if I use inSampleSize=5, which isn’t even a power of 2).

I would also like to avoid reading the image using inSampleSize with, for example, 972×648 in a first step and then resizing it to exactly 800×533 in a second step, because the quality would be poor compared to a direct resizing of the original image.

To sum up my question:
Is there a way to read a large image file with 10MP or more and save it to a new image file, resized to a specific new width and height, without getting an OutOfMemory exception?

I also tried BitmapFactory.decodeFile(file, options) and setting the Options.outHeight and Options.outWidth values manually to 800 and 533, but it doesn’t work that way.

21 Answers
21

Leave a Comment