Android parses and retrieves images on the Network (bmp format supported)

Source: Internet
Author: User

Android parses and retrieves images on the Network (bmp format supported)
Android Learning Series-display images on the Network (bmp format supported ))

See the following code:

/**
* Download the image from the Url and return it to Bitmap.
*
* @ Param imgUrl * @ return
*/
Public static Bitmap getBitmapFromUrl (String imgUrl)
{
URL url;
Bitmap bitmap = null;
Try {
Url = new URL (imgUrl );
InputStream is = url. openConnection (). getInputStream ();

BufferedInputStream bis = new BufferedInputStream (is );
// Bitmap = BitmapFactory. decodeStream (bis); Comment 1
Byte [] B = getBytes (is );
Bitmap = BitmapFactory. decodeByteArray (B, 0, B. length );
Bis. close ();
} Catch (MalformedURLException e ){
E. printStackTrace ();
} Catch (IOException e ){
E. printStackTrace ();
}
Return bitmap;
}

/**
* Convert InputStream object to Byte []
* @ Param is
* @ Return
* @ Throws IOException */

Public static byte [] getBytes (InputStream is) throws IOException {

ByteArrayOutputStream baos = new ByteArrayOutputStream ();
Byte [] B = new byte [1024];
Int len = 0;
While (len = is. read (B, 0, 1024 ))! =-1)
{
Baos. write (B, 0, len );
Baos. flush ();
}
Byte [] bytes = baos. toByteArray ();
Return bytes;
}

After obtaining Bitmap, call the setImageBitmap method of ImageView to display it normally.

PS: Note 1 here. It was originally obtained using Note 1. The png and jpg formats are normal.

However, when the image format is bmp, this method is always null.

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.