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 " "; }
新人学习中,也不太会总结,有做的不对的地方希望各位大神指出,谢谢。