java从URL获取位图
我正在尝试从URL获取位图图像
(示例:https://cdn.bulbagarden.net/upload/9/9a/Spr_7s_001.png)
但是connection.connect()
行似乎有一个问题,我无法理解
我已允许在应用程序中访问internet
public static Bitmap getBitmapFromURL(String src) {
HttpURLConnection connection = null;
InputStream inputStream = null;
try {
URL url = new URL(src);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setDoInput(true);
connection.connect();
}
return BitmapFactory.decodeStream(inputStream);
}
# 1 楼答案
你可以用glide做一个简短的解决方案
# 2 楼答案
一般来说,网络连接应该在一个单独的线程中完成。你可以用
AsyncTask
来做这个。例如,这是有效的: