Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 735
Paulo Barros Avatar asked Dec 02 '11 13:12

Paulo Barros


2 Answers

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,

like image 143
user370305 Avatar answered Oct 18 '22 17:10

user370305


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

Bitmap bitmap = BitmapFactory.decodeStream(is);
like image 26
Camille R Avatar answered Oct 18 '22 18:10

Camille R