Android reads local or network images and converts them to Bitmap_java

Source: Internet
Author: User

When doing an Android project, we often need to read a picture from a local or a network and convert it to a bitmap picture for use, and here's how to read the local picture and convert it:
Java code

/** * Gets the absolute path to the local or network bitmap URL-network or local picture, such as: * A. Network path: url= "http://blog.for 
  Everlove.us/girl2.png "; 
  * B. Local path: Url= "File://mnt/sdcard/photo/image.png"; * C. Supported picture formats, PNG, jpg,bmp,gif, etc. * * @param URL * @return/public static Bitmap Getlocalornetbitmap (St 
  Ring URL) {Bitmap Bitmap = null; 
  InputStream in = null; 
  Bufferedoutputstream out = null; 
   try {in = new Bufferedinputstream (URL). OpenStream (), constant.io_buffer_size); 
   Final Bytearrayoutputstream DataStream = new Bytearrayoutputstream (); 
   out = new Bufferedoutputstream (DataStream, constant.io_buffer_size); 
   Copy (in, out); 
   Out.flush (); 
   byte[] data = Datastream.tobytearray (); 
   Bitmap = Bitmapfactory.decodebytearray (data, 0, data.length); 
   data = null; 
  return bitmap; 
   catch (IOException e) {e.printstacktrace (); 
  return null; } 
 } 

Description: Constant.io_buffer_size is a constant, can be changed to a constant, such as 2*1024, in fact, depending on the size of your picture, your own according to the size of the image set it.

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.