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

21,985

Solution 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,

Solution 2

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

Bitmap bitmap = BitmapFactory.decodeStream(is);
Share:
21,985
Paulo Barros
Author by

Paulo Barros

Updated on July 09, 2022

Comments

  • Paulo Barros
    Paulo Barros almost 2 years

    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.

  • Paulo Barros
    Paulo Barros over 12 years
    It is not returning null anymore, it is returning a drawable object, but no picture is shown when I print it in the screen. I actually have a Blob in a database, I convert it to String, then to byte[], then to Drawable. It will be a pain to figure out where the error may be.
  • user370305
    user370305 over 12 years
    and what about if your use direct byte array to create drawable without it converting to string..?
  • user370305
    user370305 over 12 years
    And also store byte array directly in sqlite db as blob type, try this, let see what happening...