Immutable bitmap crash error

前端 未结 5 910
故里飘歌
故里飘歌 2020-12-02 10:58
java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
at android.graphics.Canvas.(Canvas.java:127)
at app.test.canvas.StartActiv         


        
相关标签:
5条回答
  • 2020-12-02 11:33

    BitmapFactory.decodeResource() returns an immutable copy of the bitmap and you cannot draw to its canvas. In order to get its canvas, you need to get a mutable copy of the images' bitmap and that can be done with single line code addition.

    opt.inMutable = true;
    

    Add that line to your code and it should address the crash.

    0 讨论(0)
  • 2020-12-02 11:37

    You have to convert your workingBitmap to Mutable Bitmap for drawing on Canvas. (Note: this method does not help save memory, it will use extra memory)

    Bitmap workingBitmap = Bitmap.createBitmap(chosenFrame);
    Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    

    This answer helps don't waste memory Convert immutable bitmap to a mutable bitmap

    0 讨论(0)
  • 2020-12-02 11:40

    Unless you don't want to make your IMMUTABLE bitmap to MUTABLE bitmap, you can save memory by ALWAYS REUSING THE BITMAP

    workingBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(workingBitmap);
    

    However I think this may be same as making the bitmap mutable by calling

    workingBitmap.isMutable = true
    
    0 讨论(0)
  • 2020-12-02 11:47

    This works as well, I just tested it.

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inMutable = true;
    return BitmapFactory.decodeByteArray(resultDecoded, 0, resultDecoded.length,options);
    
    0 讨论(0)
  • 2020-12-02 11:47

    in order to minimize memory usage, you can check out this post about converting/decoding a mutable bitmap straight from the resources :

    https://stackoverflow.com/a/16314940/878126

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