在android中从byteArray创build位图

我想从一个bytearray创build一个位图

我试了下面的代码

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

 ByteArrayInputStream bytes = new ByteArrayInputStream(data); BitmapDrawable bmd = new BitmapDrawable(bytes); bmp = bmd.getBitmap(); 

但是,当我正在初始化像位图一样的canvas对象

 Canvas canvas = new Canvas(bmp); 

它导致错误

 java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor 

然后如何从byteArray中获取可变的位图。

提前致谢。

Solutions Collecting From Web of "在android中从byteArray创build位图"

你需要一个可变的Bitmap来创buildCanvas

 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 

编辑:正如Noah Seidman所说,你可以不用创build一个副本。

 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