Resource Intensive code fix

后端 未结 1 1145
夕颜
夕颜 2021-01-29 01:49

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

1条回答
  •  北荒
    北荒 (楼主)
    2021-01-29 02:08

    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).

    0 讨论(0)
提交回复
热议问题