原文地址:Android中如何根据图片url路径来获取网络图片
1、根据图片的URL路径来获取网络图片,核心代码如下:
public static Bitmap getBitmap(String path) throws IOException{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod(“GET”);
if(conn.getResponseCode() == 200){
InputStream inputStream = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
return null;
}
2、将获得的bitmap放置到ImageView控件中:
ImageView.setImageBitmap(bitmap);
注意:记得在AndroidManifest文件中声明互联网使用权限