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

  1. public synchronized Drawable byteToDrawable(String icon) {
  2. byte[] img=Base64.decode(icon.getBytes(), Base64.DEFAULT);
  3. Bitmap bitmap;
  4. if (img != null) {
  5. bitmap = BitmapFactory.decodeByteArray(img,0, img.length);
  6. @SuppressWarnings("deprecation")
  7. Drawable drawable = new BitmapDrawable(bitmap);
  8. return drawable;
  9. }
  10. return null;
  11. }
  12. public  synchronized  String drawableToByte(Drawable drawable) {
  13. if (drawable != null) {
  14. Bitmap bitmap = Bitmap
  15. .createBitmap(
  16. drawable.getIntrinsicWidth(),
  17. drawable.getIntrinsicHeight(),
  18. drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
  19. : Bitmap.Config.RGB_565);
  20. Canvas canvas = new Canvas(bitmap);
  21. drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
  22. drawable.getIntrinsicHeight());
  23. drawable.draw(canvas);
  24. int size = bitmap.getWidth() * bitmap.getHeight() * 4;
  25. // 创建一个字节数组输出流,流的大小为size
  26. ByteArrayOutputStream baos = new ByteArrayOutputStream(size);
  27. // 设置位图的压缩格式,质量为100%,并放入字节数组输出流中
  28. bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
  29. // 将字节数组输出流转化为字节数组byte[]
  30. byte[] imagedata = baos.toByteArray();
  31. String icon= Base64.encodeToString(imagedata, Base64.DEFAULT);
  32. return icon;
  33. }
  34. return null;
  35. }

Android Drawable 和String 相互转化的更多相关文章

  1. 【转】Android Drawable Resource学习(十一)、RotateDrawable

    对另一个drawable资源,基于当前的level,进行旋转的drawable. 文件位置: res/drawable/filename.xml文件名即资源名 编译数据类型: 指向 RotateDra ...

  2. Android Drawable绘图学习笔记(转)

    如何获取 res 中的资源 数据包package:android.content.res 主要类:Resources Android SDK中的简介:Class for accessing an ap ...

  3. Android Drawable Mipmap Vector使用及Vector兼容

    原文地址:http://blog.csdn.net/eclipsexys/article/details/51838119 http://blog.csdn.net/qq_15545283/artic ...

  4. Android Drawable的9种子类 介绍

    原文: Android Drawable的9种子类 介绍   Drawable 在android里面 就是代表着图像,注意是图像 而不是图片. 图片是图像的子集.图像除了可以包含图片以外 还可以包含颜 ...

  5. CString 与 std::string 相互转化

    MFC中CString 与 std::string 相互转化 CString实际是CStringT, 也就是模板类, 在UNICODE环境下,实际是CStringW, 在多字符集环境下,实际是CStr ...

  6. Android Drawable 与 LayerList综合汇总

    先看需求.要求这样的效果 上代码 <?xml version="1.0" encoding="utf-8"? > <layer-list xm ...

  7. 【转】【Android】Android Drawable Shape 组合画田字格

    使用layer-list组合多个Shap <?xml version="1.0" encoding="utf-8"?> <layer-list ...

  8. Android Studio插件:Android Drawable Importer

    Android Drawable Importer 为了在不同分辨率的设备上更好的展示图片的效果,我们往往需要在 res/drawable 中添加不同分辨率的图片.有时我们可能手里只有一份分辨率的图片 ...

  9. 17.Letter Combinations of a Phone Number (char* 和 string 相互转化)

    leetcode 第17题 分析 char*和string相互转化 char*(或者char)转string 可以看看string的构造函数 default (1) string(); copy (2 ...

随机推荐

  1. iOS - 打电话, 发短信

    电话.短信是手机的基础功能,iOS中提供了接口,让我们调用.这篇文章简单的介绍一下iOS的打电话.发短信在程序中怎么调用. 1.打电话 [[UIApplication sharedApplicatio ...

  2. WebViewJavascriptBridge

    上一篇文章介绍了通过UIWebView实现了OC与JS交互的可能性及实现的原理,并且简单的实现了一个小的示例DEMO,当然也有一部分遗留问题,使用原生实现过程比较繁琐,代码难以维护.这篇文章主要介绍下 ...

  3. 在ios开发中有多少常用的加密解密方式(备用)

    最常用的是MD5和base64编码,还有DES 3DES AES加密 ios怎么实现RAS加密解密 最近几天折腾了一下如何在iOS上使用RSA来加密.iOS上并没有直接的RSA加密API.但是iOS提 ...

  4. asp.net中Web使用Socket

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...

  5. 关于Matlab作图的若干问题

          看到了北京一则新闻,想到如何测试双向镜子?百度之.              只要做以下简单的测试:把你的指甲尖放在镜子表面,如果在指甲尖与倒映图像之间有间隙,那就是真的镜子.然而,如果你 ...

  6. IronPython脚本调用C#dll示例

    上篇Python脚本调用C#代码数据交互示例(hello world)介绍了与C#紧密结合的示例,这里还将提供一个与C#结合更紧密的示例,直接调用C#编写的DLL.      我们还是沿用了上篇文章的 ...

  7. mysql注入攻击及防范

    一.注入攻击种类     1. GET注入         输入参数通过URL发送.     2. POST注入         输入参数通过HTTP正文发送     3. COOKIE注入      ...

  8. CISCO的HTTP/HTTPS/SSH配置测试完成

    按实验一步一步,倒是很容易的,也理解罗~~ START-CONFIG粗配置文件如下: r1#show run Building configuration... Current configurati ...

  9. 嵌套滚动demo

    https://github.com/luv135/NestedScrollingDemo https://github.com/ggajews/nestedscrollingchildviewdem ...

  10. 【HDOJ】1180 诡异的楼梯

    bfs+优先队列.wa了N次,才发现可以停留等待楼梯变换方向. #include <iostream> #include <queue> #include <cstdio ...