下載圖像,我用這個方法來下載圖像如何實現此方法,從URL中的AsyncTask
public static Bitmap getBitmapFromURL(String u, int width, int height) {
try {
URL url = new URL(u);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(input, null, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, width, height);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(input, null, options);
} catch (IOException e) {
// Log exception
return null;
}
}
這種方法和位圖適配器用於顯示每個列表項的圖像:
Bitmap bitmap = PickanteHelper.getBitmapFromURL(image_url, 150, 150);
holder.itemImage.setImageBitmap(bitmap);
爲了避免NetworkOnMainThread異常,我想我應該在加載每個圖像時使用AsyncTask?
好,你可以使用支架使用線程和交流回到UI。 itemImage作爲視圖檢查我之前回答的答案關於這些問題.. – Elltz 2014-10-18 12:25:37
不知道它是什麼,我想它像畢加索一樣是自由的?問題是我需要重新計算圖像的inSampleSize,然後才能將它設置在列表項中,以便列表滾動不會滯後 – 2014-10-18 12:26:14
您指的是我嗎? – Elltz 2014-10-18 12:28:37