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

前端 未结 2 1337
春和景丽
春和景丽 2020-12-14 18:48

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:



        
相关标签:
2条回答
  • 2020-12-14 19:16

    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,

    0 讨论(0)
  • 2020-12-14 19:36

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

    Bitmap bitmap = BitmapFactory.decodeStream(is);
    
    0 讨论(0)
提交回复
热议问题