原文:http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/

Drawable互转Bitmap

Drawable转Bitmap

1
2
3
4
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.myimage);
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();
1
2
3
4
5
6
7
8
9
10
11
12
public static Bitmap drawableToBitmap(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);
//canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}

Bitmap转Drawable

1
2
3
4
5
6
7
8
Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd=BitmapDrawable(bm);
BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。 mPicPath//本地图片路径转成Bitmap格式
Bitmap pic = BitmapFactory.decodeFile(this.mPicPath);
image.setImageBitmap(pic);
转成Bitmap格式

String与InputStream相互转换

String to InputStream

1
2
3
String str = "String与InputStream相互转换";
InputStream in_nocode = new ByteArrayInputStream(str.getBytes());
InputStream in_withcode = new ByteArrayInputStream(str.getBytes("UTF-8"));

InputStream to String

这里提供几个方法。

方法1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public String convertStreamToString(InputStream is) {   
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder(); String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "/n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} return sb.toString();
}

方法2:

1
2
3
4
5
6
7
8
public   String   inputStream2String   (InputStream   in)   throws   IOException   { 
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}

方法3:

1
2
3
4
5
6
7
8
public   static   String   inputStream2String(InputStream   is)   throws   IOException{ 
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i=-1;
while((i=is.read())!=-1){
baos.write(i);
}
return baos.toString();
}

Bitmap 和 byte[]互转

Bitmap → byte[]

1
2
3
4
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray(); }

byte[] → Bitmap

1
2
3
4
5
6
7
8
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}

【转】Drawable /Bitmap、String/InputStream、Bitmap/byte[]的更多相关文章

  1. Android Drawable 和String 相互转化

    在我们经常应用开发中,经常用到将drawable和string相互转化.注意这情况最好用于小图片入icon等. public synchronized Drawable byteToDrawable( ...

  2. BitmapUtil【缩放bitmap以及将bitmap保存成图片到SD卡中】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 用于缩放bitmap以及将bitmap保存成图片到SD卡中 效果图 代码分析 bitmapZoomByHeight(Bitmap s ...

  3. <BitMap>大名鼎鼎的bitmap算法

    BitMap 抛砖引玉 首先,我们思考一个问题:如何在3亿个整数(0~2亿)中判断某一个数是否存在?现在只有一台机器,内存只有500M 这个问题像不像我们之前提到过的一个在0-10个数中,判断某一个数 ...

  4. String inputStream file转化

    String --> InputStreamByteArrayInputStream stream = new ByteArrayInputStream(str.getBytes()); Inp ...

  5. java常用string inputStream转换

    1.String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); 或者 ByteArrayInp ...

  6. java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230

    近期遇到了如标题这种错误,再次记录解决方法.本文參考帖子: http://bbs.csdn.net/topics/390196217 出现此bug的原因是在内存回收上.里面用Bitamp的代码为: t ...

  7. java 中String编码和byte 解码总结——字节流和字符流

    1.InputStreamReader 是字节流通向字符流的桥梁:它使用指定的 charset 读取字节并将其解码为字符 InputStreamReader(InputStream in, Strin ...

  8. Canvas: trying to use a recycled bitmap android.graphics.Bitmap@XXX

    近期在做和图片相关显示的出现了一个问题,整理一下思路.分享出来给大家參考一下: Exception Type:java.lang.RuntimeException java.lang.RuntimeE ...

  9. Android java.lang.RuntimeException: Canvas: trying to use a recycled bitmap android.graphics.Bitmap@412d7230

    近期遇到了如标题这种错误,再次记录解决方法.本文參考帖子: http://bbs.csdn.net/topics/390196217 出现此bug的原因是在内存回收上.里面用Bitamp的代码为: t ...

随机推荐

  1. Markdown 11种基本语法

    markdown真的很方便! 我根据自己在github上维护的项目编写的readme文件总结一些最基本的使用方法 ,学完这些Markdown的基本使用已经不成问题. 1. 标题设置(让字体变大,和wo ...

  2. android开发环境搭建(ubuntu15.04+jdk8+eclipse+android sdk)

    开始学习android开发,首先对其环境对搭建比较重要.平台可以选择window/linux/mac等,这里,我选择ubuntu系统,方法比较原始,当然也可以直接用google提供的android s ...

  3. [HTTP Protocol] 200 OK (from cache)和304 Not Modified

    含义 200 OK (from cache)直接从缓存中获取的内容并未请求服务器 304 Not Modified 请求服务器并和服务器比较 If-Modified-Since,若文件未改变,服务器返 ...

  4. Delphi dll 断点调试

    1.dll 要有一个依托的exe(怎么做 相信用dll了一定知道) 2.选项中的compling中的debugging中的选项,linking中的所有选项 3.最后一个也就是最重要的 run中的par ...

  5. MFC的多线程操作

    记得用MFC做了一个图像自动修复软件,当时没有多线程操作这一概念,由于图像修复算法比较复杂,因此,当执行图像修复时,程序就像卡死了似得而不能做其他操作.其实MFC对这种情况有一种很好地解决方案,那就是 ...

  6. MYSQL(一)

    一,概述: 1,什么是数据库: 答:数据的仓库,如:在ATM的示例中我们创建了一个 db 目录,称其为数据库. 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Serv ...

  7. UVa 11987 Almost Union-Find(支持删除操作的并查集)

    传送门 Description I hope you know the beautiful Union-Find structure. In this problem, you’re to imple ...

  8. iOS 个人账号 iOS APP Development 灰色不可选

    如图,现在的开发者账号是有几个人共用的,已经 生成了一个Development 的证书,我想再申请一个,出现了这样的情况.网上有说的是申请证书个数到了上限,需要删除已经生成的.因为生成的证书其他人需要 ...

  9. input file控件限制上传文件类型

    网页上添加一个input file HTML控件: <input id="File1" type="file" /> 默认是这样的,所有文件类型都会 ...

  10. 10月28日下午MySQL数据库的增加、删除、查询(匹配数据库登录和可以增、删、查的显示数据库内容的页面))

    一.匹配数据库登录 步骤: 1.做一个普通的登录界面,注意提交方式为post. <!--登录界面--> <form action="chuli.php" meth ...