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