
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;
}
/**
* 将InputStream对象转换为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;
}
得到Bitmap 之后。然后调用ImageView的setImageBitmap方法就正常显示了
PS:凝视1这里注意一下。原本是用凝视1这里来进行获取的,png,jpg格式均正常
,可是图片格公式bmp时刻。当该方法已经被获取null, 因此,在现在这样的方式。
版权声明:本文博主原创文章,博客,未经同意不得转载。