Android学习系列 - 显示网络上的图片(支持bmp格式))
见如下代码:
/**
* 到Url地址上去下载图片,并回传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); 注释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;
}
见如下代码:
/**
* 到Url地址上去下载图片,并回传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); 注释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;
}