0

AsyncTaskインターネットからいくつかのファイルをダウンロードするによって管理されるプログレスバーがあります。

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


            }

ファイルをダウンロードできるようにする方法:

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


            }

このコードはうまく機能しますが、問題は、ダウンロードされた各ファイルに独自のプログレスバーがあることです。ダウンロードされたすべてのファイルに対して1つのプログレスバーだけが必要です。

どうすればこれを達成できますか?どうもありがとうございます

4

2 に答える 2

0

非同期タスクに複数のURLを渡します。

new DownloadImgs().execute(URL1,URL2,URL3);

進行状況全体が単一の進行状況バーとして表示されます。

于 2013-01-01T04:12:31.227 に答える
0

あなたのdoInBackground()方法では、あなたの単一の実行の中であなたがダウンロードする必要があるファイルのリストを単にループしますAsyncTask。おそらく、varargsの性質を利用して、execute()ダウンロードする必要のあるURLのリストを渡す必要があります。言い換えれば、これに近いもの:

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

タスクを次のように変更します。

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 */
}

編集:

すべてのファイルのダウンロードを単一の連続した進行状況インジケーターとして表示するために使用できる1つの方法は、ファイルの内容ではなく、ファイル数だけで進行状況を更新することです。つまり、publishProgress()値が20(1 * 100/5)、40(2 * 100/5)、60(3 * 100/5)、80(4 * 100/5)、および各ファイルのダウンロードの最後に100(5 * 100/5)。

すべてのファイルのコンテンツの長さをプリフェッチせずに、よりきめ細かいものが必要な場合は、各チャンクをパーセンテージで増分します。プログレスバーの最大レベルをsetMax()100以外に設定して、計算を簡単にすることもできます。同じ5つのファイルの例では、プログレスバーの最大値を500に設定できます。ダウンロードごとに、現在と同じ方法で合計に100を追加しますが、各ダウンロードの開始時に合計はリセットされません。 。

于 2013-01-01T04:26:49.597 に答える