Android transforms the image into stream storage and converts the flow to an image

Source: Internet
Author: User

1. Convert picture to binary stream

 Public byte [] Getbitmapbyte (Bitmap Bitmap) {      new  bytearrayoutputstream ();       // parameter 1 conversion type, parameter 2 compression quality, parameter 3 byte throttle resource    Bitmap.compress (Bitmap.CompressFormat.JPEG,N, out);       Try {          out.flush ();          Out.close ();       Catch (IOException e) {          e.printstacktrace ();      }       return Out.tobytearray ();  }  

2. Change the binary stream into a picture (BITMAP)

 Public Bitmap getbitmapfrombyte (byte[] temp) {      ifnull) {          = Bitmapfactory.decodebytearray (temp, 0, temp.length);           return bitmap;      } Else {          returnnull;      }  }  

Swap binary streams for pictures (drawable)

 Public Drawable getbitmapfrombyte (byte[] temp) {      ifnull) {          = Drawable.createfromstream (Bais, "image");         return drawable;      } Else {          returnnull;      }  }  

3. Convert Bitmap to Drawable

 Public StaticBitmap Drawabletobitmap (drawable drawable) {intwidth =drawable.getintrinsicwidth (); intHeight =drawable.getintrinsicheight (); Bitmap Bitmap=bitmap.createbitmap (width, height, drawable.getopacity ()! = Pixelformat.opaque?Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565); Canvas Canvas=NewCanvas (bitmap); Drawable.setbounds (0,0, Width,height);                  Drawable.draw (canvas); returnbitmap; }    

Or

New Fastbitmapdrawable (bitmap);  

Or


Bitmapdrawable tempdrawable = (bitmapdrawable) drawable; Tempdrawable.getbitmap ();

Android transforms the image into stream storage and converts the flow to an image

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.