从asynctask更新Imageview

时间:2014-01-30 10:49:13

标签: android android-asynctask

我已经有了一个带有ImageView的列表视图,然后在AsyncTask(我的应用程序中任何类型的文件的通用下载程序)的帮助下开始为它下载图像列表。 每个下载的图像后,我需要将其插入ImageView。所以我认为我需要从AsyncTask到主线程的某种回调,但如何做到这一点我不知道?

这是我的AsyncTask代码

DownloadFileFromURL down =(DownloadFileFromURL)new DownloadFileFromURL(Destination).execute(“http://example.com”);

public class DownloadFileFromURL extends AsyncTask<String, String, String> {

private String Destination;

public DownloadFileFromURL(String UrlDestination){
    this.Destination=UrlDestination;
}

/**
 * Before starting background thread Show Progress Bar Dialog
 * */
@Override
protected void onPreExecute() {
    super.onPreExecute();

}

/**
 * Downloading file in background thread
 * */
@Override
protected String doInBackground(String... f_url) {
    int count;
    try {
        URL url = new URL(f_url[0]);
        URLConnection connection = url.openConnection();
        connection.connect();
        // this will be useful so that you can show a tipical 0-100%
        // progress bar
        int lenghtOfFile = connection.getContentLength();
        // download the file
        InputStream input = new BufferedInputStream(url.openStream(),8192);
        // Output stream

        Log.d("City.xml:",this.Destination);

        File targetFile = new File(this.Destination);
        File parent = targetFile.getParentFile();
        if(!parent.exists() && !parent.mkdirs()){
            Log.e("Error: ", ": " + parent);
        }


        //OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/"+this.Destination);
        OutputStream output = new FileOutputStream(targetFile);

        byte data[] = new byte[1024];

        long total = 0;

        while ((count = input.read(data)) != -1) {
            total += count;
            // publishing the progress....
            // After this onProgressUpdate will be called
            publishProgress("" + (int) ((total * 100) / lenghtOfFile));

            // writing data to file
            output.write(data, 0, count);
        }

        // flushing output
        output.flush();

        // closing streams
        output.close();
        input.close();

    } catch (Exception e) {
        Log.e("Error: ",  e.getMessage());
    }

    return null;
}

/**
 * Updating progress bar
 * */
protected void onProgressUpdate(String... progress) {
    // setting progress percentage
}

/**
 * After completing background task Dismiss the progress dialog
 * **/
@Override
protected void onPostExecute(String file_url) {
    // dismiss the dialog after the file was downloaded

}

}

4 个答案:

答案 0 :(得分:1)

查看AsyncTask documentation

onPostExecute(result)调用方法UI Thread,以便您可以修改ListView

更新:

您可以创建界面:

interface IOnFileDownloadedListener{

    public void onFileDownloaded(String filePath); // or type other than string
}

然后在你的班级DownloadFileFromURL中,你可以以某种方式传递实现IOnFileDownloadedListener的类(例如在构造函数中)。

然后在下载文件后,您可以检查侦听器是否为空(如果您决定不允许传递任何侦听器),如果它不为null,则可以调用onFileDownloaded

public class DownloadFileFromURL extends AsyncTask<String, String, String> {

private String Destination;
private IOnFileDownloadedListener Listener = null;

public DownloadFileFromURL(String UrlDestination){
    this.Destination=UrlDestination;
}

public DownloadFileFromURL(String UrlDestination, IOnFileDownloadedListener FDListener){
    this(UrlDestination);
    Listener = FDListener;
}

...

@Override
protected void onPostExecute(String file_url) {
    if(this.Listener != null) this.Listener.onFileDownloaded(this.Destination);
}

答案 1 :(得分:0)

您可以使用库 Picasso

轻松地从服务器加载图像

http://square.github.io/picasso/

以下是一个例子:

Picasso.with(context).load("http://yoururlserver.com/yourimage.png").into(imageView);

答案 2 :(得分:0)

答案 3 :(得分:0)

我建议使用Android通用图像加载程序库(https://github.com/nostra13/Android-Universal-Image-Loader)。

您可以轻松加载:

String imageUri = "http://www.yoursite.com/image.png"; // Image url
imageLoader.displayImage(imageUri, imageView); // imageView - your imageView