//將字元串轉化成Drawable public synchronized static Drawable StringToDrawable(String icon) { if (icon == null || icon.length() < 10) return null; byte[] img ... ...
//將字元串轉化成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 " "; }