Android bitmap usage, never worry about it again.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.