//将字符串转化成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之间互相转化的更多相关文章

  1. Gson进行json字符串和对象之间的转化

    Gson可以实现对象与json字符串之间的转化,以下是在Android中的示例代码. Gson主页:https://code.google.com/p/google-gson/ public clas ...

  2. C# .net中json字符串和对象之间的转化方法

    http://blog.csdn.net/xuexiaodong009/article/details/46998069 json作为作为一种最常用的数据,应用很广泛,在.net中如何把一个对象转化为 ...

  3. js数字、字符串、数组之间的转化

    1.数组转字符串 var a, b; a = ,,,,); b = a.join("-"); 2.字符串转数组 var s = "abc,abcd,aaa"; ...

  4. Android中Bitmap, Drawable, Byte,ID之间的转化

    Android中Bitmap, Drawable, Byte,ID之间的转化 1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...

  5. C#入门篇6-6:字符串操作 StringBiulder string char[]之间的转化

    //StringBiulder string char[]之间的转化 public static void Fun3() { StringBuilder sb = new StringBuilder( ...

  6. json对象和json字符串之间的转化

    json对象和json字符串之间的转化 json字符串----->json对象 使用JSON.parse()函数 var jsonStr = '{"name":"z ...

  7. jQuery对象和DOM对象和字符串之间的转化

    jQuery对象和DOM对象和字符串之间的转化 字符串---------->jQuery对象 $(HTML字符串): $('<div>我是祖国的一朵小花</div>') ...

  8. java对象与Json字符串之间的转化(fastjson)

    1. 首先引入jar包 在pom.xml文件里加入下面依赖: <dependency> <groupId>com.alibaba</groupId> <art ...

  9. Android图片二进制与Bitmap、Drawable之间的转换

    Android图片二进制与Bitmap.Drawable之间的转换 Java代码  public byte[] getBitmapByte(Bitmap bitmap){      ByteArray ...

随机推荐

  1. Ubuntu 16.04 安装 arm-linux-gcc 交叉编译工具

    工作需要,最近在编译linux嵌入式内核时,需要安装arm-linux-gcc交叉编译,实际上,安装这个交叉编译器的难度没啥.不过,这里有些问题还是值得我去思考和记录下来的. 这个系统的上的编译器用的 ...

  2. Spring Boot 核心配置文件 bootstrap & application 详解。

    用过 Spring Boot 的都知道在 Spring Boot 中有以下两种配置文件 bootstrap (.yml 或者 .properties) application (.yml 或者 .pr ...

  3. 自定义线程池reject策略

    import java.util.Date;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.Rej ...

  4. Ubuntu16.04安装Anaconda (转)

    一. Anaconda下载 Anaconda 官方下载链接: https://www.continuum.io/downloads 根据自己的系统选择下载32位还是64位.   二. 进入下载目录 如 ...

  5. IMSI

    国际移动用户识别码(IMSI:International Mobile Subscriber Identification Number)是区别移动用户的标志,储存在SIM卡中,可用于区别移动用户的有 ...

  6. C# sqlhelper 整理

    以下代码是参考几个不同人的写法总结写成的,肯定还有很大的优化空间,暂存该版本:有建议的欢迎提出: using System; using System.Collections.Generic; usi ...

  7. mac使用brew安装mysql

    1.安装mysql #brew install mysql 报错 Error: The following directories are not writable by your user: /us ...

  8. Linux 源码安装 FFmpeg

    加班次数频繁,已经快一年没有写博了.由于此次在项目中使用到了 FFmpeg 来处理音视频,顾记录下在 Linux 系统中源码安装 FFmpeg 以便不时之需. 1. 安装 yasm yasm 是一个汇 ...

  9. varnish实践

    一.实验环境: 1.软件版本: 系统版本:CentOS Linux release 7.4.1708 (Core) php版本:PHP 7.2 nginx版本:nginx-1.12.2 数据库版本:M ...

  10. 【转载】To the Virgins, to Make Much of Time

    Gather ye rosebuds while ye may Old Time is still a-flying And this same flower that smiles today To ...