Android Bitmap, bitmapdrawable, stream conversion integration

Source: Internet
Author: User

Package Com.soai.imdemo;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.inputstream;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.pixelformat;import android.graphics.drawable.BitmapDrawable; Import android.graphics.drawable.drawable;/** * bitmap and drawable and byte[] and InputStream conversion Tool class * @author Administrator * * /public class Formattools {private static formattools tools = new Formattools ();p ublic static Formattools getinstance () {i F (Tools = = null) {tools = new Formattools (); return tools;} return tools; Convert byte[] to inputstreampublic inputstream Byte2inputstream (byte[] b) {Bytearrayinputstream Bais = new Bytearrayinputstream (b); return Bais;} Convert InputStream to Byte[]public byte[] Inputstream2bytes (InputStream is) {String str = ""; byte[] readbyte = new byte[1024]; int readcount = -1;try {while ((Readcount = Is.read (readbyte, 0, 1024x768))! =-1) {str + = new String (readbyte). Trim (); REturn str.getbytes ();} catch (Exception e) {e.printstacktrace ();} return null;} Convert Bitmap to Inputstreampublic inputstream Bitmap2inputstream (Bitmap BM) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Bm.compress (Bitmap.CompressFormat.JPEG, BAOs); InputStream is = new Bytearrayinputstream (Baos.tobytearray ()); return is;} Convert Bitmap to Inputstreampublic inputstream Bitmap2inputstream (Bitmap BM, int quality) {Bytearrayoutputstream BAOs = new B Ytearrayoutputstream (); bm.compress (Bitmap.CompressFormat.PNG, quality, BAOs); InputStream is = new Bytearrayinputstream (Baos.tobytearray ()); return is;} Convert InputStream to Bitmappublic Bitmap Inputstream2bitmap (InputStream is) {return Bitmapfactory.decodestream (is);} Drawable converted to Inputstreampublic inputstream Drawable2inputstream (drawable d) {Bitmap Bitmap = This.drawable2bitmap (d); return this. Bitmap2inputstream (bitmap);} InputStream converted to Drawablepublic drawable inputstream2drawable (InputStream is) {Bitmap Bitmap = this. Inputstream2bitmap (is); reTurn this.bitmap2drawable (bitmap);} Drawable converted to Byte[]public byte[] Drawable2bytes (drawable d) {Bitmap Bitmap = This.drawable2bitmap (d); return this. Bitmap2bytes (bitmap);} Byte[] converted to Drawablepublic drawable bytes2drawable (byte[] b) {Bitmap Bitmap = this. Bytes2bitmap (b); return this.bitmap2drawable (bitmap);} Bitmap converted to Byte[]public byte[] Bitmap2bytes (Bitmap BM) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Bm.compress (Bitmap.CompressFormat.PNG, BAOs); return Baos.tobytearray ();} Byte[] Convert to Bitmappublic Bitmap Bytes2bitmap (byte[] b) {if (b.length! = 0) {return Bitmapfactory.decodebytearray (b, 0, B.L Ength);} return null;} Drawable converted to Bitmappublic Bitmap Drawable2bitmap (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);d rawable.setbounds (0, 0, drawable.getintrinsicwidth (), drawable.geTintrinsicheight ());d Rawable.draw (canvas); return bitmap;} Bitmap converted to Drawablepublic drawable bitmap2drawable (Bitmap Bitmap) {bitmapdrawable bd = new bitmapdrawable (Bitmap);D Rawable d = (drawable) Bd;return D;}}


Build your own framework and start with a little bit

Android Bitmap, bitmapdrawable, stream conversion integration

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.