How to create a Drawable from byte[] ? (Android)

自古美人都是妖i 提交于 2019-12-29 04:14:27

问题


I have an array of bytes and I need to convert it into a Android Drawable. How can I perform this conversion?

Here is what i tried but without success:

byte[] b = getByteArray();
ByteArrayInputStream is = new ByteArrayInputStream(b);
Drawable drw = Drawable.createFromStream(is, "articleImage");

drw is always null!

EDIT:

My byte[] was actually corrupted/incomplete, that was the problem.


回答1:


If your byte[] b is contains imagedata then you can also try this,

 Drawable image = new BitmapDrawable(BitmapFactory.decodeByteArray(b, 0, b.length));

EDIT

BitmapDrawable constructor without Resources is now deprecated, So use this instead:

Drawable image = new BitmapDrawable(getResources(),BitmapFactory.decodeByteArray(b, 0, b.length));

Try this and let me know what happen,




回答2:


Do you really need a Drawable ? If Bitmap can fit, then :

Bitmap bitmap = BitmapFactory.decodeStream(is);


来源:https://stackoverflow.com/questions/8357309/how-to-create-a-drawable-from-byte-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!