多次下载异步

时间:2013-01-16 16:23:58

标签: java android multithreading android-asynctask

在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);
    }

3 个答案:

答案 0 :(得分:4)

@Override
protected Bitmap doInBackground(ImageView... imageViews) {
    this.imageView = imageViews[0];
    return download_Image((String)imageView.getTag());
}

您只处理发送到AsyncTask的第一个参数。使用参数ImageView...,您可以发送一个,两个,N个元素或数组。循环遍历参数并处理每个参数。

您可以在doInBackground中使用onPostExecute的回复,我个人尽量为encapsulation使用它。

总体而言,我建议您阅读有关Android开发者的AsyncTask参考,以便更好地了解AsyncTasks。

我确实重写了你的代码,下面理论上应该可行(它没有经过测试)。

public class DownloadImageTaskDAO extends AsyncTask<ImageView, Void, ArrayList<DownloadImageTaskDAO.Container>> {

    @Override
    protected ArrayList<Container> doInBackground(ImageView... imageViews) {
         ArrayList<Container> results = new ArrayList<Container>(imageViews.length);

         for (ImageView imageView : imageViews) {
             Container tmp = new Container();
             tmp.holder = imageView;
             tmp.image = download_Image(imageView.getTag().toString());
             results.add(tmp);
         }

         return results;
    }

    @Override
    protected void onPostExecute(ArrayList<Container> result) {
        for (Container container : result) {
            container.holder.setImageBitmap(container.image);
        }
    }

    private Bitmap download_Image(String url) {
        ....
    }

    class Container {
        ImageView holder;
        Bitmap image;
    }
}

答案 1 :(得分:1)

不要使用AsyncTask,而是考虑将IntentService与作为服务额外传递的ResultReceiver一起使用。 IntentService在加载时表现更好(比如下载多个图像),因为它们使用HandlerThread而不是使用ThreadPoolExecutor的AsyncTasks。

IntentService

ResultReceiver

答案 2 :(得分:0)

我建议用这种方式来实现你的代码

希望您通过单Multiple images次电话下载AsyncTask会有所帮助。

Asynctask Call Method

public void setImages(String[] urls)
{
   DownloadImageTaskDAO imageDAO = new DownloadImageTaskDAO();

   if(int i= 0; i< urls.lenght();i++)
   {
        image.setTag(urls[i]);
        imageDAO.execute(image).get();
    }
}

DownloadImageTaskDAO Class:

public class DownloadImageTaskDAO extends AsyncTask<ImageView, Void, Bitmap> {

    ImageView imageView = null;

    @Override
    protected Bitmap doInBackground(ImageView imageView) {
        this.imageView = imageView;
        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;
        //---------------------------------------------------
    }

}