I’m trying to write a program for the Android platform where I deal with images a lot.
Consider the following:
Bitmap b = BitmapFactory.decodeFile(File imagePath) // 5mb picture is stored into memory as with 32bits of memory per pixel --> almost run out of memory
I will want to change the pixels in this Bitmap, but for some reason the Bitmap returned is immutable. I figured that making a copy of it using b.copy(Config config, boolean isMutable)
would do the trick, although I run into a RuntimeExeception saying that the VM won’t let us allocate blah blah bytes of memory.
My question is, would the following line
Bitmap mutable = BitmapFactory.decodeFile(File imagePath).copy(config, true);
allocate less memory and therefore be safely used to get my mutable copy of the Bitmap, given that there is no reference to the original immutable Bitmap?
This seems to do the job sometimes, but I would like an explanation of whether I’m just talking nonsense or if what I’m doing is a clever workaround to achieve my task.
No. The copy method has a reference to the immutable bitmap while copying it to the mutable one. This reference is called
this. So you still have a reference to both bitmaps at the same time.The
Optionsobject that you can pass to the BitMapFactorydecodeFilemethod has aninMutableoption, though, which would avoid the immutable bitmap completely.