在java中,我想一次为我的imageview下载多个图像。我发现这段代码确实如此,但它只下载第一个图像。我认为这与 doInBackground 中的第一行有关。如何调整此代码,使其循环 imageViews 并下载 imageViews 的每个图像。
package com.denederlandsewateren.daos;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.ImageView;
public class DownloadImageTaskDAO extends AsyncTask<ImageView, Void, Bitmap> {
ImageView imageView = null;
@Override
protected Bitmap doInBackground(ImageView... imageViews) {
this.imageView = imageViews[0];
return download_Image((String)imageView.getTag());
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
}
private Bitmap download_Image(String url) {
//---------------------------------------------------
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
}
return bm;
//---------------------------------------------------
}
}
从这里调用:
public void setImages(String url1, String url2) {
image1.setTag(url1);
image2.setTag(url2);
DownloadImageTaskDAO imageDAO = new DownloadImageTaskDAO();
imageDAO.execute(image1, image2);
}