Bitmap,byte[],drawable,inputstream Mutual Conversion Tool class in Android

Source: Internet
Author: User

In Sina to see some of the conversion of data flow, share:

1. Convert byte[] to InputStream

    Public InputStream Byte2inputstream (byte[] b) {          Bytearrayinputstream Bais = new Bytearrayinputstream (b);          return Bais;      }  


2. Convert InputStream to byte[]
Two different ways:
One
public static final byte[] Inputstream2bytes (InputStream instream) {    Bytearrayoutputstream swapstream = new Bytearrayoutputstream ();    byte[] buff = new byte[100];    int rc = 0;    try {while  (rc = instream.read (buff, 0,)) > 0) {  swapstream.write (buff, 0, RC);  }  } catch (Ioexce Ption e) {  //TODO auto-generated catch block  e.printstacktrace ();  }    byte[] in2b = Swapstream.tobytearray ();    return in2b;    }


Two
Public byte[] Inputstream2bytes (inputstream) {          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;      }  



3. Convert Bitmap to InputStream
Public InputStream Bitmap2inputstream (Bitmap BM, int quality) {          Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();          Bm.compress (Bitmap.CompressFormat.PNG, quality, baos);          InputStream is = new Bytearrayinputstream (Baos.tobytearray ());          return is;      }  



4. Convert InputStream to bitmap
Public Bitmap Inputstream2bitmap (InputStream are) {          return Bitmapfactory.decodestream (IS);      }  



5, drawable converted into InputStream
   Public InputStream Drawable2inputstream (drawable d) {          Bitmap Bitmap = This.drawable2bitmap (d);          return this. Bitmap2inputstream (bitmap);      }  



6, InputStream converted into drawable
Public drawable inputstream2drawable (InputStream are) {          Bitmap Bitmap = this. Inputstream2bitmap (is);          Return this.bitmap2drawable (bitmap);      }  



7, drawable converted into byte[]
    Public byte[] Drawable2bytes (drawable d) {          Bitmap Bitmap = This.drawable2bitmap (d);          return this. Bitmap2bytes (bitmap);      }  


8, byte[] converted into drawable
  Public drawable bytes2drawable (byte[] b) {          Bitmap Bitmap = this. Bytes2bitmap (b);          Return this.bitmap2drawable (bitmap);      }  



9, bitmap converted into byte[]
    Public byte[] Bitmap2bytes (Bitmap BM) {          Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();          Bm.compress (Bitmap.CompressFormat.PNG, BAOs);          return Baos.tobytearray ();      }  



10, byte[] converted into bitmap
    Public Bitmap Bytes2bitmap (byte[] b) {          if (b.length! = 0) {              return Bitmapfactory.decodebytearray (b, 0, B.length) ;          }          return null;      }  



11, drawable converted into bitmap
Public 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);          Drawable.setbounds (0, 0, drawable.getintrinsicwidth (),                  drawable.getintrinsicheight ());          Drawable.draw (canvas);          return bitmap;      }  



12, bitmap converted into drawable

    Public drawable bitmap2drawable (Bitmap Bitmap) {          bitmapdrawable bd = new bitmapdrawable (Bitmap);          drawable d = (drawable) BD;          return D;      


Bitmap,byte[],drawable,inputstream Mutual Conversion Tool class in Android

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.