6.2

我们都知道,在Android开发中,会遇到要请求服务器拿到图片的一种情况,这种情况又怎么进行处理,我主要就是整理了下面的一些简单方法。

其实总之就是,要得到图片的URL,而不是直接得到图片的一种方式(这样处理就可能存在URL改变了,那么图片就无法显示)。

// 传输网络图片
public Bitmap getPic(String uriPic) {
URL imageUrl = null;
Bitmap bitmap = null;
try {
imageUrl = new URL(uriPic);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) imageUrl
.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);

is.close();

} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

 

posted @ 2024-06-06 22:29  chrisrmas、  阅读(2)  评论(0编辑  收藏  举报