Common types of Android conversions

Source: Internet
Author: User

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

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.