1, 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);d rawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ());d Rawable.draw (canvas); return bitmap;}
2. Get bitmap from the resources
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, 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;}}
5. Save Bitmap
Static Boolean savebitmap2file (Bitmap bmp,string filename) {Compressformat format= bitmap.compressformat.jpeg;int quality = 100;outputstream stream = null;try {stream = new FileOutputStream ("/sdcard/" + filename);} catch (Filenotfoundex Ception e) {//TODO auto-generated catch blockgenerated by Foxit PDF creator©foxit softwarehttp://www.foxitsoftware.com For evaluation only.e.printstacktrace ();} return bmp.compress (format, quality, stream);}
6, the picture according to their own requirements to zoom
Picture Source Bitmap BM = Bitmapfactory.decodestream (Getresources (). Openrawresource (R.drawable.dog));//Get the picture's width high int width = Bm.getwidth (); int height = bm.getheight ();//Set the desired size int newwidth = 320;int Newheight = 480;//calculate the scaling float ScaleWidth = ((f loat) newwidth)/Width;float ScaleHeight = ((float) newheight)/height;//get the Matrix parameter you want to scale matrix matrix = new Matrix (); MA Trix.postscale (ScaleWidth, ScaleHeight);//Get new picture Bitmap NEWBM = bitmap.createbitmap (BM, 0, 0, width, height, matrix,true );//Put on canvas canvas.drawbitmap (NEWBM, 0, 0, paint);
7:file Pictures Turn bitmap
Bitmap BT = Bitmapfactory.decodefile ("/sdcard/myimage/" + "head.jpg");//Picture address
8://Pictures turn bitmap
Public Bitmap drawabletobitamp (int drawableresource) {<span style= "White-space:pre" ></span>// You can take resources from raw bitmapfactory.options opt = new bitmapfactory.options (); opt.inpreferredconfig = Bitmap.config.rgb_565;o pt.inpurgeable = True;opt.ininputshareable = True;inputstream is = This.getresources (). Openrawresource ( Drawableresource); Bitmapfactory.decodestream (is, null, opt); return Bitmapfactory.decodestream (IS, null, opt);}
Android bitmap usage, never worry about it again.