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.