Android中Bitmap,byte[],Drawable,InputStream相互转化工具类
2015-10-26 22:21
489 查看
1、将byte[]转换成InputStream
public InputStream Byte2InputStream(byte[] b) {ByteArrayInputStream bais = new ByteArrayInputStream(b);
return bais;
}
2、 将InputStream转换成byte[]
两种方式:
一、
public
static final byte[] InputStream2Bytes(InputStream inStream) {
ByteArrayOutputStream
swapStream = new ByteArrayOutputStream();
byte[]
buff = new byte[100];
int
rc = 0;
try
{
while ((rc
= inStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff,
0, rc);
}
} catch (IOException
e) {
// TODO Auto-generated
catch block
e.printStackTrace();
}
byte[]
in2b = swapStream.toByteArray();
return
in2b;
}
二、
public byte[] InputStream2Bytes(InputStream is) {
String str = "";
byte[] readByte = new byte[1024];
int readCount = -1;
try {
while ((readCount = is.read(readByte, 0, 1024)) != -1) {
str += new String(readByte).trim();
}
return str.getBytes();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
3、 将Bitmap转换成InputStream
public InputStream Bitmap2InputStream(Bitmap bm, int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, quality, baos);
InputStream is = new ByteArrayInputStream(baos.toByteArray());
return is;
}
4、 将InputStream转换成Bitmap
public Bitmap InputStream2Bitmap(InputStream is) {
return BitmapFactory.decodeStream(is);
}
5、 Drawable转换成InputStream
public InputStream Drawable2InputStream(Drawable d) {
Bitmap bitmap = this.drawable2Bitmap(d);
return this.Bitmap2InputStream(bitmap);
}
6、 InputStream转换成Drawable
public Drawable InputStream2Drawable(InputStream is) {
Bitmap bitmap = this.InputStream2Bitmap(is);
return this.bitmap2Drawable(bitmap);
}
7、 Drawable转换成byte[]
public byte[] Drawable2Bytes(Drawable d) {
Bitmap bitmap = this.drawable2Bitmap(d);
return this.Bitmap2Bytes(bitmap);
}
8、 byte[]转换成Drawable
public Drawable Bytes2Drawable(byte[] b) {
Bitmap bitmap = this.Bytes2Bitmap(b);
return this.bitmap2Drawable(bitmap);
}
8、 Bitmap转换成byte[]
public byte[] Bitmap2Bytes(Bitmap bm) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
9、 byte[]转换成Bitmap
public Bitmap Bytes2Bitmap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
return null;
}
10、Drawable转换成Bitmap
public Bitmap drawable2Bitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
11、 Bitmap转换成Drawable
public Drawable bitmap2Drawable(Bitmap bitmap) {
BitmapDrawable bd = new BitmapDrawable(bitmap);
Drawable d = (Drawable) bd;
return d;
}
相关文章推荐
- Android ffmpeg+librtmp+speex交叉编译
- Android_10_javah命令
- Android控件之RadioGroup、RadioButton
- 我的Android进阶之旅------>android中service的onStartCommand()方法中intent为null的问题
- android消息推送
- Android上面通过URL来启动本地应用
- android studio使用发布者证书调试
- Android中measure过程、view绘制原理和MeasureSpec介绍及使用详解
- 【FastDev4Android框架开发】Android崩溃异常捕捉CustomCrash,提升用户体验(五)
- Android获取控件大小的方法
- [Android应用开发] 05.广播和服务
- 安卓开发技术:监听软键盘的显示与隐藏
- 手机开发-Android
- android的控件详解autoCompleteContextTextView和MultiAutoCompleteContextTextView
- Android 理解生命周期最好例子—按home暂停音乐播放,跳转暂停
- Android之自定义ViewPagerIndicator
- Android简单的聊天室开发(client与server沟通)
- android实现超链接的3中方法
- android orhanobut logger打印那个类第几行调用log输出,在输出日志上上点击跳转到源代码
- Android如何创建背景透明的Dialog