Android BASE64 encoding and decoding

Source: Internet
Author: User

Service-side picture information is converted to a string, to the Android client, the Android side need to decode this information into a picture and saved locally.   Encode part of String string = Base64.encodetostring (Str.getbytes (), base64.default);       Decoding part of string is the information sent by the server byte[] byteicon= base64.decode (String,base64.default); for (int i = 0; i < byteicon.length; ++i) {   if (Byteicon[i] < 0) {Byteicon[i] + = 256; }}//Create a File Object iconfile = "New File" ("Usericon.png");    FileOutputStream fos = new FileOutputStream (iconfile); if (!iconfile.exists ()) {iconfile.creatnewfile (); }//the picture data into a file to form a picture fos.write (Byteicon); I went to the internet to search, found that my writing has become more complex, in fact, Byteicon can be directly converted to bitmap, but this will not be able to achieve the local save//server//code, Remember this stream, often used for images and Base64 data switching bytearrayoutputstream BAOs = new Bytearrayoutputstream ();//Picture is compressed and converted into a stream, Bitmap Here I do not initialize the bitmap.compress (Bitmap.compressformat,100,baos); byte[] Byteserver = Baos.tobytearray (); String result = base64.encodetostring (ByteServer.Base64.DEFAULT); [Enter a link description here] [1]//android end byte[] Byteicon = Base64.decode (Result,base64.default); Bitmap Bitmap = Bitmapfactory.decode (byteicon,0,byteicon.length);

Android Base64 encoding decoding

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.