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);
(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);
相关文章推荐
- 【Android】Bitmap、InputStream、Drawable、byte[]、Base64之间的相互转换
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Bitmap和Drawable之间的相互转换、Bitmap和 byte[] 之间的相互转换、从资源中获取Bitmap
- android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系
- Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- 【android】bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类
- android Bitmap与Drawable与byte[]与InputStream之间的转换工具类
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
- Bitmap与DrawAble与byte[]与InputStream之间的转换工具类