Conversion between android bitmap and base64 strings

Source: Internet
Author: User

Conversion between android bitmap and base64 strings

Bitmap images and base64 strings can be converted to each other ~ Since then, my mom no longer has to worry about processing bitmap ~

/**

* Convert bitmap to a base64 string

*

* @ Param bitmap

* @ Return base64 string

*/

Public String bitmaptoString (Bitmap bitmap, int bitmapQuality ){

// Convert Bitmap into a string

String string = null;

ByteArrayOutputStream bStream = new ByteArrayOutputStream ();

Bitmap. compress (CompressFormat. PNG, bitmapQuality, bStream );

Byte [] bytes = bStream. toByteArray ();

String = Base64.encodeToString (bytes, Base64.DEFAULT );

Return string;

}

/**

* Convert base64 to bitmap image

*

* @ Param string base64 string

* @ Return bitmap

*/

Public Bitmap stringtoBitmap (String string ){

// Convert the string to the Bitmap type

Bitmap bitmap = null;

Try {

Byte [] bitmapArray;

BitmapArray = Base64.decode (string, Base64.DEFAULT );

Bitmap = BitmapFactory. decodeByteArray (bitmapArray, 0,

BitmapArray. length );

} Catch (Exception e ){

E. printStackTrace ();

}

Return bitmap;

}

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.