Android parsing gets pictures on the network (BMP format supported)

Source: Internet
Author: User

Android Learning Series-Display images on the Web (BMP format supported))

See the following code:

/**
* Go to the URL to download the image and return 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); Note 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 objects 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 getting bitmap, then call ImageView's Setimagebitmap method to display the normal

PS: NOTE 1 Notice here, originally used in note 1 here to obtain, png,jpg format is normal

, but the image format is BMP, this method gets the time is always null, it is now this way.

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.