1 //convert bitmap to InputStream (compression rate quality, 100 means no compression, 10 for compression 90%)2 PublicInputStream Bitmap2inputstream (Bitmap BM,intquality) {3Bytearrayoutputstream BAOs =NewBytearrayoutputstream ();4 bm.compress (Bitmap.CompressFormat.PNG, quality, baos);5InputStream is =NewBytearrayinputstream (Baos.tobytearray ());6 returnis ;7 }8 9 //convert bitmap to InputStreamTen PublicInputStream Bitmap2inputstream (Bitmap BM) { OneBytearrayoutputstream BAOs =NewBytearrayoutputstream (); ABm.compress (Bitmap.CompressFormat.JPEG, 100, BAOs); -InputStream is =NewBytearrayinputstream (Baos.tobytearray ()); - returnis ; the } - - //convert InputStream to bitmap - PublicBitmap Inputstream2bitmap (InputStream is) { + returnBitmapfactory.decodestream (IS); - } + A //drawable conversion into InputStream at PublicInputStream Drawable2inputstream (drawable d) { -Bitmap Bitmap = This. Drawable2bitmap (d); - return This. Bitmap2inputstream (bitmap); - } - - //InputStream conversion into drawable in Publicdrawable inputstream2drawable (InputStream is) { -Bitmap Bitmap = This. Inputstream2bitmap (is); to return This. bitmap2drawable (bitmap); + } - the //drawable Convert to byte[] * Public byte[] drawable2bytes (drawable d) { $Bitmap Bitmap = This. Drawable2bitmap (d);Panax Notoginseng return This. Bitmap2bytes (bitmap); - } the + //byte[] Convert to drawable A PublicDrawable bytes2drawable (byte[] b) { theBitmap Bitmap = This. Bytes2bitmap (b); + return This. bitmap2drawable (bitmap); - } $ $ //bitmap Convert to byte[] - Public byte[] bitmap2bytes (Bitmap BM) { -Bytearrayoutputstream BAOs =NewBytearrayoutputstream (); theBm.compress (Bitmap.CompressFormat.PNG, 100, BAOs); - returnBaos.tobytearray ();Wuyi } the - //byte[] Convert to bitmap Wu PublicBitmap Bytes2bitmap (byte[] b) { - if(B.length! = 0) { About returnBitmapfactory.decodebytearray (b, 0, b.length); $ } - return NULL; - } - A //convert byte[] to InputStream + PublicInputStream Byte2inputstream (byte[] b) { theBytearrayinputstream Bais =NewBytearrayinputstream (b); - returnBais; $ } the the //convert InputStream to byte[] the Public byte[] Inputstream2bytes (InputStream is) { theString str = ""; - byte[] ReadByte =New byte[1024]; in intReadcount =-1; the Try { the while((Readcount = Is.read (readbyte, 0, 1024))! =-1) { Aboutstr + =NewString (readbyte). Trim (); the } the returnstr.getbytes (); the}Catch(Exception e) { + e.printstacktrace (); - } the return NULL;Bayi } the the //drawable conversion into bitmap - PublicBitmap Drawable2bitmap (drawable drawable) { -Bitmap Bitmap =Bitmap.createbitmap (Drawable.getintrinsicwidth (), theDrawable.getintrinsicheight (), drawable.getopacity ()! = Pixelformat.opaque?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565); theCanvas Canvas =NewCanvas (bitmap); theDrawable.setbounds (0, 0, Drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); the Drawable.draw (canvas); - returnbitmap; the } the the //bitmap conversion into drawable94 Publicdrawable bitmap2drawable (Bitmap Bitmap) { thebitmapdrawable BD =Newbitmapdrawable (bitmap); thedrawable d =(drawable) BD; the returnD;98 } About - //convert bitmap to Base64101 PublicString getimgstr (Bitmap bit) {102Bytearrayoutputstream bos=NewBytearrayoutputstream ();103Bit.compress (Compressformat.jpeg, N, BOS);//parameter 100 means no compression104 byte[] bytes=Bos.tobytearray (); the returnbase64.encodetostring (bytes, base64.default);106 } 107 108 //convert Base64 to bitmap109 PublicBitmap getimg (String str) { the byte[] bytes;111Bytes=base64.decode (str, 0); the returnBitmapfactory.decodebytearray (Bytes, 0, bytes.length);113}
Recently encountered a lot of conversion relationship, so just put it down to share with you ...
The bitmap, InputStream, drawable, byte[], and Base64 transitions between Android-related things