Convert drawable to bitmap
Public static bitmap drawabletobitmap (drawable) {// obtain the length and width of drawable int W = drawable. getintrinsicwidth (); int H = drawable. getintrinsicheight (); // retrieves the drawable color format bitmap. config = drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888: bitmap. config. rgb_565; // create the corresponding bitmapbitmap bitmap = bitmap. createbitmap (W, H, config); // create the canvas = new canvas (Bitmap); drawable. setbounds (0, 0, W, H); // draw the drawable content into the canvas. draw (canvas); Return bitmap ;}Obtain bitmap from resources
Resources res = getResources();Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
Bitmap → byte []
public byte[] Bitmap2Bytes(Bitmap bm) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);return baos.toByteArray();}
Byte [] → bitmap
public Bitmap Bytes2Bimap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);} else {return null;}}
Bitmap to drawable
Bitmap Bm = xxx; // XXX obtain bitmapdrawable BD = new bitmapdrawable (getresource (), BM) based on your situation. Because btimapdrawable is a subclass of drawable, you can directly use the BD object.