Android加载网络图片资源
获取图片资源
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;
}
加载图片
ImageView img = findViewById(R.id.img);
try {
img.setImageBitmap(getBitmap("https://www.yyanxt.cn/images/9KJ9Hb/tmp_129628b733480afaa2c0473c08de44628422aa77c79ca59e.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
效果