1.drawable->bitmap
Resources res=getresources ();
Bitmap Bmp=bitmapfactory.decoderesource (res, R.DRAWABLE.SAMPLE_0);
Resources res=getresources ();
Private byte[] Bitmap2bytes (Bitmap BM) {
2.Bitmap---->drawable
Drawable drawable =new bitmapdrawable (BMP);
3, Drawable→bitmap
public static Bitmap Drawabletobitmap (drawable 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;
}
4. Get bitmap from the resources
Bitmap Bmp=bitmapfactory.decoderesource (res, r.drawable.pic);
5, bitmap→byte[]
Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
Bm.compress (Bitmap.CompressFormat.PNG, BAOs);
return Baos.tobytearray (); }
6, Byte[]→bitmap
Private Bitmap Bytes2bimap (byte[] b) {
if (b.length!=0) {
Return Bitmapfactory.decodebytearray (b, 0, b.length);
}
else {
return null;
}
}
How bitmap and drawable in Android convert to each other