使用 URLConnection 直接 decodeStream 返回 null,ImageView 空白。
public static Bitmap downloadBitmap(String urlString) {
HttpGet request = new HttpGet(urlString);
request.setHeader("User-Agent", "Android App");
try (CloseableHttpClient client = HttpClients.createDefault();
CloseableHttpResponse response = client.execute(request)) {
int status = response.getStatusLine().getStatusCode();
if (status != HttpStatus.SC_OK) return null;
HttpEntity entity = response.getEntity();
if (entity == null) return null;
try (InputStream is = entity.getContent()) {
return BitmapFactory.decodeStream(is);
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Glide.with(this).load(url).into(imageView);