I have a large bitmap (say 3888x2592) in a file. Now, I want to resize that bitmap to 800x533 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 972x648 (if I use inSampleSize=4
) or to 778x518 (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, 972x648 in a first step and then resizing it to exactly 800x533 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.
Justin answer translated to code (works perfect for me):
This worked for me. The function gets a path to a file on the sd card and returns a Bitmap in the maximum displayable size. The code is from Ofir with some changes like image file on sd instead a Ressource and the witdth and heigth are get from the Display Object.
Here is the code I use which doesn't have any issues decoding large images in memory on Android. I have been able to decode images larger then 20MB as long as my input parameters are around 1024x1024. You can save the returned bitmap to another file. Below this method is another method which I also use to scale images to a new bitmap. Feel free to use this code as you wish.
NOTE: Methods have nothing to do with each other except createScaledBitmap calls decode method above. Note width and height can change from original image.
Above code made a little cleaner. InputStreams have finally close wrapping to ensure they get closed as well:
*Note
Input: InputStream is, int w, int h
Output: Bitmap
After reading these answers and android documentation here's the code to resize bitmap without loading it into memory:
If you absolutely want to do one step resize you could probably load entire bitmap if android:largeHeap = true but as you can see this is not really advisable.
From docs: android:largeHeap Whether your application's processes should be created with a large Dalvik heap. This applies to all processes created for the application. It only applies to the first application loaded into a process; if you're using a shared user ID to allow multiple applications to use a process, they all must use this option consistently or they will have unpredictable results. Most apps should not need this and should instead focus on reducing their overall memory usage for improved performance. Enabling this also does not guarantee a fixed increase in available memory, because some devices are constrained by their total available memory.