下载一个进度条的文件 [英] download files in a single progress bar

查看:123
本文介绍了下载一个进度条的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有管理的进度条的AsyncTask 它从互联网上下载一些文件。

I have a progress bar managed by a AsyncTask which downloads some files from the internet.

private class DownloadImgs extends AsyncTask<Void, String, Void> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                showDialog(progress_bar_type);
            }


            @Override
            protected Void doInBackground(Void ...params) {
               for(GetCountry gc : listCountry){
           getdownloadedFiles(gc.getUrl());}

           return null;

            }


            protected void onProgressUpdate(String... progress) {

                pDialog.setProgress(Integer.parseInt(progress[0]));
           }

            @Override
            protected void onPostExecute(Void result) {

                dismissDialog(progress_bar_type);


            }

这让我下载的文件的方法:

The method which allows me to download the files:

public void getdownloadedFiles(String url)

            {

                int count;
                 try {

                  URL urlImg = new URL(url);
                  URLConnection connection = urlImg.openConnection();
                  connection.connect();

                  int lenghtOfFile = connection.getContentLength();
                  InputStream input = new BufferedInputStream(urlImg.openStream(), 8192);


                  OutputStream output = new FileOutputStream(folder+"/"+name);

                  byte data[] = new byte[1024];

                  long total = 0;

                  while ((count = input.read(data)) != -1) {
                      total += count;

                     publishProgress(""+(int)((total*100)/lenghtOfFile));

                      output.write(data, 0, count);

                  }


                  output.flush();           
                  output.close();
                  input.close();

                 } catch (Exception e) {
                     e.getMessage();
                 }


            }

这code效果很好,但问题是,下载的每个文件都有它自己的进度条
我想对所有下载的文件只有一个进度条。

This code works well, but the problem is that each file downloaded has it's own progress bar I want only one progress bar for all the downloaded files.

我怎样才能做到这一点?
非常感谢你。

How can I achieve this? Thank you very much

推荐答案

在你的 doInBackground()方法,只需在您需要的内部下载的文件列表循环你的的AsyncTask 的单执行。你应该还利用的可变参数性质的execute()传给你需要下载的URL列表。换句话说,这个东西收盘:

In your doInBackground() method, simply loop over the list of files you need to download inside of the single execution of your AsyncTask. You should probably also leverage the varargs nature of execute() to pass the list of URLs you need to download. In other words, something close to this:

DownloadImgs task = new DownloadImgs();
task.execute(url1, url2, url3, url4, url5);

的修改任务这样:

The modify the task as such:

private class DownloadImgs extends AsyncTask<String, String, Void> {

    @Override
    protected Void doInBackground(String ...params) {

        for (String url : params) {
            getdownloadedFiles(url);
        }

        return null;
    }

    public void getdownloadedFiles(String url) {
        /* Existing code */
    }

    /* Other methods unchanged */
}

编辑:

您可以用它来显示所有文件下载可以作为一个连续进度指示器是简单地通过更新文件中进步单独算,而不是文件内容的一种方法。换句话说,如果你有5个文件只是 publishProgress()为20(1×100/5),40(2 * 100/5),60个值(3 * 100/5),80(4 * 100/5),和100(5 * 100/5)在每个文件下载结束

One method you could use to display all the file downloads as a single contiguous progress indicator is simply to update the progress by file count alone rather than file content. In other words, if you have 5 files just publishProgress() with values of 20 (1 * 100 / 5), 40 (2 * 100 / 5), 60 (3 * 100 / 5), 80 (4 * 100 / 5), and 100 (5 * 100 / 5) at the end of each file download.

如果您需要在没有pre-获取每个文件的内容长度的东西更精细,使与百分比每个块增量。请注意,您还可以使用 SETMAX()设置进度栏的最高水平,以超过100以外的东西,使数学更容易使用。在同样的5档例如,您可以在进度条最大值设置为500,并为每个下载你仍然会增加100总以同样的方式你现在的样子,但总不能在每次下载的开始复位

If you need something more granular without pre-fetching the content length of every file, make each chunk increment with percentage. Note that you can also set the maximum level of the progress bar with setMax() to something other than 100 to make the math easier to work with. In the same 5 files example, you can set the progress bar maximum to 500, and for each download you would still add 100 to the total in the same fashion as you are now, but the total does not reset at the beginning of each download.

这篇关于下载一个进度条的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆