I have the following piece of code running on a picture. It produces the following error. I am assuming it is because the code takes a few seconds to run and then crashes so mus
setPixels
requires a mutable Bitmap, otherwise you will get an IllegalStateException
. See documentation.
You should copy or create a new bitmap, and make it mutable:
To create a new mutable bitmap:
Bitmap newBitmap = Bitmap.createBitmap(originalWidth, originalHeight, Bitmap.Config.ARGB_8888);
Or to copy and make a mutable bitmap:
boolean isMutable = true;
Bitmap newBitmap = originalBitmap.copy(Bitmap.Config.ARGB_8888, isMutable);
Now you will be able to use newBitmap.setPixels(...)
If this is an expensive operation, you should consider doing it in a background thread (e.g. AsyncTask
).