这是一个用于加载图像的简单功能。它在 android 2.2 上运行良好,但它不会下载和显示任何适用于 android 4 的图像。这是创建的代码:
Bitmap bitmap = DownloadImage(weather.icon);
holder.imgIcon.setImageBitmap(bitmap);
持有人工作正常/这是功能:
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
BufferedInputStream bis = new BufferedInputStream(in, 8190);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
byte[] imageData = baf.toByteArray();
bitmap = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length);
in.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
谁能帮帮我,我很累。谢谢