Android--字符串和Drawable之间互相转化
//将字符串转化成Drawable
public synchronized static Drawable StringToDrawable(String icon) {
if (icon == null || icon.length() < 10)
return null;
byte[] img = Base64.decode(icon.getBytes(), Base64.DEFAULT);
Bitmap bitmap;
if (img != null) {
bitmap = BitmapFactory.decodeByteArray(img, 0, img.length);
@SuppressWarnings("deprecation")
Drawable drawable = new BitmapDrawable(bitmap); return drawable;
}
return null;
} //将drawable转化成字符串
public synchronized static String DrawableToString(Drawable drawable) {
if (drawable != null) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
int size = bitmap.getWidth() * bitmap.getHeight() * 4; // 创建一个字节数组输出流,流的大小为size
ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
// 设置位图的压缩格式,质量为100%,并放入字节数组输出流中
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
// 将字节数组输出流转化为字节数组byte[]
byte[] imagedata = baos.toByteArray(); return Base64.encodeToString(imagedata, Base64.DEFAULT);
}
return " ";
}
Android--字符串和Drawable之间互相转化的更多相关文章
- Gson进行json字符串和对象之间的转化
Gson可以实现对象与json字符串之间的转化,以下是在Android中的示例代码. Gson主页:https://code.google.com/p/google-gson/ public clas ...
- C# .net中json字符串和对象之间的转化方法
http://blog.csdn.net/xuexiaodong009/article/details/46998069 json作为作为一种最常用的数据,应用很广泛,在.net中如何把一个对象转化为 ...
- js数字、字符串、数组之间的转化
1.数组转字符串 var a, b; a = ,,,,); b = a.join("-"); 2.字符串转数组 var s = "abc,abcd,aaa"; ...
- Android中Bitmap, Drawable, Byte,ID之间的转化
Android中Bitmap, Drawable, Byte,ID之间的转化 1. Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...
- C#入门篇6-6:字符串操作 StringBiulder string char[]之间的转化
//StringBiulder string char[]之间的转化 public static void Fun3() { StringBuilder sb = new StringBuilder( ...
- json对象和json字符串之间的转化
json对象和json字符串之间的转化 json字符串----->json对象 使用JSON.parse()函数 var jsonStr = '{"name":"z ...
- jQuery对象和DOM对象和字符串之间的转化
jQuery对象和DOM对象和字符串之间的转化 字符串---------->jQuery对象 $(HTML字符串): $('<div>我是祖国的一朵小花</div>') ...
- java对象与Json字符串之间的转化(fastjson)
1. 首先引入jar包 在pom.xml文件里加入下面依赖: <dependency> <groupId>com.alibaba</groupId> <art ...
- Android图片二进制与Bitmap、Drawable之间的转换
Android图片二进制与Bitmap.Drawable之间的转换 Java代码 public byte[] getBitmapByte(Bitmap bitmap){ ByteArray ...
随机推荐
- Shell-5--位置参数变量
- Testing - 软件测试知识梳理 - 测试分类
参考信息 软件测试分类 经典软件测试技术分类 软件测试方法汇总 简洁分类 对软件内部结构的深入程度 黑盒测试:又叫功能测试.数据驱动测试或基于需求规格说明书的功能测试. 该测试类别注重于测试软件的功能 ...
- 参考信息 - Serverless
初见 Serverless的本质是什么? 看懂 Serverless,这一篇就够了 关于「Serverless」的完整指南:你知道和不知道的 了解 7个开源平台,入门无服务器计算
- eclipse连接远程服务器
eclipse里有一个强大的插件,可以直接在本地编辑远程服务器代码,Eclipse Remote System Explorer (RSE) 下载安装方法: 一.下载,高版本的eclipse可以直接下 ...
- conda环境里安装pydot
一.conda环境里安装pydot, 输入以下命令即可: conda install -c anaconda pydot 二.如果运行tensorflow,提示缺什么包,都可以在这里下载. ----- ...
- xlwt set style making error: More than 4094 XFs (styles)
使用Xlwt,当内容过多时,会报错:More than 4094 XFs (styles) 解决方法: wb = xlwt.Workbook(style_compression=2) 使用style_ ...
- Thread,ThreadPool,Task, 到async await 的基本使用方法和理解
很久以前的一个面试场景: 面试官:说说你对JavaScript闭包的理解吧? 我:嗯,平时都是前端工程师在写JS,我们一般只管写后端代码. 面试官:你是后端程序员啊,好吧,那问问你多线程编程的问题吧. ...
- RC1015 cannot open include file 'atlres.h'
fatal error RC1015: cannot open include file 'atlres.h' 问题:此问题是由于rc文件没有找到 atlres.h导致的 (原因不详) 解决:工程 ...
- Linux_CentOS-服务器搭建 <七>
设置Linux下Mysql表名不区分大小写 对linux安装mysql不熟悉的(查看我那一系列的文章第一篇): http://www.cnblogs.com/Alandre/p/3365535.htm ...
- TCP/IP 笔记 - 传输控制协议
与UDP不同,TCP提供面向连接的.可靠的.基于字节流的传输层协议,且提供差错纠正. TCP传输的概念 对与分组丢失和比特差错的处理方法,最直接的方法是重发分组,直到它被正确接收. 这需要一种方法来判 ...