您的位置:首页 > 其它

BitMap,inputStream,byte[],Drawable之间的相互转换

2016-08-19 09:46 525 查看
在Android开发中,有时候我们会得到一个byte[],但是我们需要的是一个Drawable,或者是一个BitMap,那么我们该如何转换呢?

(1) BitMap  to   inputStream:

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

    InputStream isBm = new ByteArrayInputStream(baos .toByteArray());
 (2)BitMap  to   byte[]:

  Bitmap defaultIcon = BitmapFactory.decodeStream(in);

   ByteArrayOutputStream stream = new ByteArrayOutputStream();

   defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, stream);

  byte[] bitmapdata = stream.toByteArray();
 (3)Drawable  to   byte[]:

Drawable d; // the drawable (Captain Obvious, to the rescue!!!)

  Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

   ByteArrayOutputStream stream = new ByteArrayOutputStream();

  defaultIcon.compress(Bitmap.CompressFormat.JPEG, 100, bitmap);

  byte[] bitmapdata = stream.toByteArray();
(4)byte[]  to  Bitmap :

  Bitmap bitmap =BitmapFactory.decodeByteArray(byte[], 0,byte[].length);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐