It is feasible and tested in person
Turn: http://blog.csdn.net/hexingzhi/article/details/7598567
1. Bitmap to drawable bitmap Bm = xxx; // XXX obtain bitmapdrawable BD = new bitmapdrawable (BM) based on your situation, because btimapdrawable is a subclass of drawable, you can directly use the BD object. 2. After drawable is converted into a bitmap object through bitmap, The drawable object can be converted into a byte output stream through the android SK inventory, and finally saved as JPG and PNG files. Drawable d = xxx; // XXX obtain drawable bitmapdrawable BD = (bitmapdrawable) D according to your own situation; bitmap Bm = BD. getbitmap (); finally, BM is the bitmap object we need. // Obtain bitmap public static bitmap getbitmapfromresources (Activity Act, int resid) {resources res = act. getresources (); Return bitmapfactory. decoderesource (Res, resid);} // byte [] → bitmap public static bitmap convertbytes2bimap (byte [] B) {If (B. length = 0) {return NULL;} return bitmapfactory. decodebytearray (B, 0, B. length);} // bitmap → byte [] public static byte [] convertbitmap2bytes (bit Map BM) {bytearrayoutputstream baos = new bytearrayoutputstream (); BM. compress (bitmap. compressformat. PNG, 100, baos); Return baos. tobytearray ();} // 1) drawable → bitmap public static bitmap convertdrawable2bitmapbycanvas (drawable) {Bitmap bitmap = bitmap. createbitmap (drawable. getintrinsicwidth (), drawable. getintrinsicheight (), drawable. getopacity ()! = Pixelformat. opaque? Bitmap. config. argb_8888: bitmap. config. rgb_565); canvas = new canvas (Bitmap); // canvas. setbitmap (Bitmap); drawable. setbounds (0, 0, drawable. getintrinsicwidth (), drawable. getintrinsicheight (); drawable. draw (canvas); Return bitmap;} // 2) drawable → bitmap public static bitmap convertdrawable2bitmapsimple (drawable) {bitmapdrawable BD = (bitmapdrawable) drawable; return BD. getbitma P () ;}// bitmap → drawable public static drawable convertbitmap2drawable (Bitmap bitmap) {bitmapdrawable BD = new bitmapdrawable (Bitmap); // btimapdrawable is a subclass of drawable, you can directly use the BD object. Return BD ;}
Private bitmap getviewbitmap (view ){
View. clearfocus ();
View. setpressed (false );
Boolean willnotcache = view. willnotcachedrawing ();
View. setwillnotcachedrawing (false );
Int color = view. getdrawingcachebackgroundcolor ();
View. setdrawingcachebackgroundcolor (0 );
If (color! = 0 ){
View. destroydrawingcache ();
}
View. builddrawingcache ();
Bitmap cachebitmap = view. getdrawingcache ();
If (cachebitmap = NULL ){
Return NULL;
}
Bitmap bitmap = bitmap. createbitmap (cachebitmap );
View. destroydrawingcache ();
View. setwillnotcachedrawing (willnotcache );
View. setdrawingcachebackgroundcolor (color );
Return bitmap;
}