create Bitmap from byteArray in android

前端 未结 1 780
礼貌的吻别
礼貌的吻别 2020-12-05 09:19

I want to create a bitmap from a bytearray .

I tried the following codes

Bitmap bmp;

bmp = BitmapFactory.decodeByteArray(data, 0, data.length);


        
相关标签:
1条回答
  • 2020-12-05 10:17

    You need a mutable Bitmap in order to create the Canvas.

    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
    Bitmap mutableBitmap = bmp.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap); // now it should work ok
    

    Edit: As Noah Seidman said, you can do it without creating a copy.

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inMutable = true;
    Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length, options);
    Canvas canvas = new Canvas(bmp); // now it should work ok
    
    0 讨论(0)
提交回复
热议问题