您的位置:首页 > 理论基础 > 计算机网络

Android在网络上分析获取图片(支持bmp格式)

2015-10-27 08:07 489 查看
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, 因此,在现在这样的方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: