在android中从byteArray创建位图 [英] create Bitmap from byteArray in android

查看:32
本文介绍了在android中从byteArray创建位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从字节数组创建位图.

I want to create a bitmap from a bytearray .

我尝试了以下代码

Bitmap bmp;

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

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

但是,当我想用​​位图初始化 Canvas 对象时

But ,When i am tring to initialize the Canvas object with the bitmap like

Canvas canvas = new Canvas(bmp);

导致错误

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

然后如何从 byteArray 获取可变位图.

Then how to get a mutable bitmap from an byteArray.

提前致谢.

推荐答案

你需要一个可变的 Bitmap 来创建 Canvas.

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

正如 Noah Seidman 所说,您无需创建副本即可完成.

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

这篇关于在android中从byteArray创建位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆