Converting an immutable bitmap to be mutable in Android

By: Kevin | June 11, 2014

I ran into this error while I was working with a custom view that allowed users to draw on a image using their finger.

[java]java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor[/java]

I found the quick fix is to just make a copy of the original bitmap and pass a true boolean which is the flag to make the copy mutable.

[java]
Bitmap immutableBitmap = coming from where ever your source is….
Bitmap mutableBitmap = immutableBitmap.copy(Bitmap.Config.ARGB_8888, true);//<–true makes copy mutable
Canvas canvas = new Canvas(mutableBitmap);
[/java]

Easy peasy!

 

Documentation here.