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.