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

来源:互联网 发布:网络渗透基础 编辑:程序博客网 时间:2024/05/16 17:17

在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);

0 0
原创粉丝点击