這是一個簡單的加載圖像功能。它在Android 2.2上運行良好,但它不下載並顯示任何圖像的android 4。 這是創建代碼:爲什麼Android 4沒有下載並顯示來自url的圖像
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;
}
任何人都可以幫助我,這是把我累壞。 謝謝
是的,你可以通過這種方式修復它,但這只是禁用限制。你應該修復這個問題:在UI線程上下載。 – RvdK