Android Image base64 encoding decoding

Source: Internet
Author: User
Tags base64

Android to encode picture decoding demo

 PackageCom.example.appdemos;ImportJava.io.ByteArrayOutputStream;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;ImportAndroid.graphics.Bitmap.CompressFormat;Importandroid.graphics.BitmapFactory;ImportAndroid.os.Bundle;Importandroid.util.Base64;ImportAndroid.widget.ImageView; Public classBaseactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.base_main); Bitmap Bitmap=Bitmapfactory.decoderesource (Getresources (), r.drawable.add); String String=getBitmapStrBase64 (bitmap); Bitmap bitmaps=Stringtobitmap (string); ImageView img=(ImageView) Findviewbyid (r.id.img);    Img.setimagebitmap (bitmaps); }    /*** Bitmap converted to string by Base64 *@paramBitmap *@return     */    PrivateString getBitmapStrBase64 (Bitmap Bitmap) {bytearrayoutputstream BOS=NewBytearrayoutputstream (); Bitmap.compress (Compressformat.png,100, BOS); byte[] bytes =Bos.tobytearray (); String String=base64.encodetostring (bytes, base64.default); returnstring; }        /*** String Conversion bitmap *@paramSTR *@return     */    PrivateBitmap stringtobitmap (String str) {byte[] input =NULL; Input=Base64.decode (str, base64.default); Bitmap Bitmap= Bitmapfactory.decodebytearray (input, 0, input.length); returnbitmap; }}

Android Image base64 encoding decoding

Related Article

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.