1. drawable-> bitmap
Resources res = getresources ();
Bitmap BMP = bitmapfactory. decoderesource (Res, R. drawable. sample_0 );
2. Bitmap ----> drawable
Drawable = new bitmapdrawable (BMP );
In addition, I searched the internet and I don't know who is the original one.
1. drawable → bitmap
Public static bitmap drawabletobitmap (drawable ){
Bitmap bitmap = bitmap. createbitmap (
Drawable. getintrinsicwidth (),
Drawable. getintrinsicheight (),
Drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888
: Bitmap. config. rgb_565 );
Canvas canvas = new canvas (Bitmap );
// Canvas. setbitmap (Bitmap );
Drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight ());
Drawable. Draw (canvas );
Return bitmap;
}
2. Obtain bitmap from a resource
Resources res = getresources ();
Bitmap BMP = bitmapfactory. decoderesource (Res, R. drawable. PIC );
3. Bitmap → byte []
Private byte [] bitmap2bytes (Bitmap BM ){
Bytearrayoutputstream baos = new bytearrayoutputstream ();
BM. Compress (bitmap. compressformat. PNG, 100, baos );
Return baos. tobytearray ();}
4. byte [] → bitmap
Private bitmap bytes2bimap (byte [] B ){
If (B. length! = 0 ){
Return bitmapfactory. decodebytearray (B, 0, B. Length );
}
Else {
Return NULL;
}
}