This article is a record of the types of conversions that we usually use during Android development, including the conversion between string, byte[], bitmap, Inputstram, drawable, and the code is as follows:
<span style= "FONT-SIZE:18PX;" >import Java.io.bufferedreader;import Java.io.bytearrayinputstream;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.inputstreamreader;import Android.graphics.Bitmap; Import Android.graphics.bitmapfactory;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;public class Demo {/** * bitmap to byte[] array */public byte[] Bitmap2bytearray (bitmap bit Map) {Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); Bitmap.compress (Bitmap.CompressFormat.PNG, +, BAOs); byte[] bytes = Baos.tobytearray (); return bytes;} /** * Bitmap Turn InputStream */public inputstream bitmap2inputstream (bitmap bitmap) {Bytearrayoutputstream BAOs = new ByteAr Rayoutputstream (); Bitmap.compress (Bitmap.CompressFormat.PNG, BAOs); InputStream is = new Bytearrayinputstream ( Baos.tobytearray ()); return is;} /** * byte[] array turn bitmap */public bitmap bytearray2bitmap (byte[] bytes) {Bitmap bitmap = bitmapFactory.decodebytearray (bytes, 0, bytes.length); return bitmap;} /** * InputStream turn bitmap */public bitmap Inputstream2bitmap (InputStream is) {Bitmap bitmap = Bitmapfactory.decodestre AM (IS); return bitmap;} /** * drawable turn bitmap */public bitmap drawable2bitmap (drawable img) {bitmapdrawable BD = (bitmapdrawable) img; Bitmap Bitmap = Bd.getbitmap (); return Bitmap;} /** * Bitmap Turn drawable */public drawable bitmap2drawable (bitmap bitmap) {bitmapdrawable bd = new bitmapdrawable (bitmap);D rawable img = Bd;return img;} /** * string to byte[] array */public byte[] String2bytearray (String str,string charset) {byte[] bytes = null;if (CharSet = nul L) {bytes = Str.getbytes ();} Else{try {//= CharSet = "Utf-8" bytes = Str.getbytes (charset);} catch (Exception e) {//Todo:handle Exception}}return bytes ;} /** * String to InputStream */public inputstream string2inputstream (String str) {InputStream is = new Bytearrayinputstream ( Str.getbytes ()); return is;} /** * InputStream Turn string method */public string InputStream2STRING01 (InputStream is) throws Ioexception{bytearrayoutputstream BAOs = new Bytearrayoutputstream (); int i = -1;while ((i =is.read ())! =-1) {baos.write (i);} return baos.tostring ();} /** * InputStream Turn string method */public string Inputstream2string02 (InputStream is) throws Ioexception{bufferedreader br = new BufferedReader (new InputStreamReader (IS)); StringBuilder sb = new StringBuilder (); String line = Null;while ((line=br.readline ()) = null) {sb.append (line+ "\ n");} return sb.tostring ();} /** * InputStream Turn string method */public string inputsteam2string03 (InputStream is) throws Ioexception{stringbuilder SB = new StringBuilder (); byte[] b =new byte[1024];for (int n; (N=is.read (b))! =-1;) {string s = new String (b, 0, N); Sb.append (s);} return sb.tostring ();}} </span>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Common types of Android conversions