Android-conversion between string and Drawable, android-drawable

Source: Internet
Author: User

Android-conversion between string and Drawable, android-drawable

// Convert the String to Drawable public synchronized static Drawable StringToDrawable (String icon) {if (icon = null | icon. length () <10) return null; byte [] img = Base64.decode (icon. getBytes (), Base64.DEFAULT); Bitmap bitmap; if (img! = Null) {bitmap = BitmapFactory. decodeByteArray (img, 0, img. length); @ SuppressWarnings ("deprecation") Drawable drawable = new BitmapDrawable (bitmap); return drawable;} return null ;} // convert drawable to public synchronized static String DrawableToString (Drawable drawable) {if (drawable! = Null) {Bitmap bitmap = Bitmap. createBitmap (drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (), drawable. getOpacity ()! = PixelFormat. OPAQUE? Bitmap. config. ARGB_8888: Bitmap. config. ARGB_8888); Canvas canvas = new Canvas (bitmap); drawable. setBounds (0, 0, drawable. getIntrinsicWidth (), drawable. getIntrinsicHeight (); drawable. draw (canvas); int size = bitmap. getWidth () * bitmap. getHeight () * 4; // create a byte array of output streams. The stream size is ByteArrayOutputStream baos = new ByteArrayOutputStream (size). // set the bitmap compression format, it has a quality of 100% and is put into the byte array output stream bitmap. compress (Bitmap. compressFormat. PNG, 100, baos); // convert byte array output to byte array byte [] byte [] imagedata = baos. toByteArray (); return Base64.encodeToString (imagedata, Base64.DEFAULT);} return "";}

 

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.