1

我想在一个进度条中下载多个文件(目前为 2 个)(不重置进度条)

为了做到这一点,我检索了所有文件的总长度,以便在发布进度方法中使用它

问题是我的进度条一旦达到 50% 就会重置为 0 然后再次增加直到另一个 50%

这是我的代码

@Override
        protected Void doInBackground(Void... params) {
            int totalSizeFile;
            totalSizeFile=cm.getLength(url[0]);
            totalSizeFile+=cm.getLength(url[1]);
            progressBar.setMax(totalSizeFile);

            cm.downloadMp3(url[0], "test.mp3", totalTailleFic);
            cm.downloadMp3(url[1], "test2.mp3", totalTailleFic);


            return null;            

        }

        @Override
        protected void onProgressUpdate(Integer... progress) {

            textview.setText(String.valueOf(progress[0])+"%");
            progressBar.incrementProgressBy(progress[0]);


        }

我调用 publishprogress 方法的下载函数的代码

byte[] buffer = new byte[1024];
            int bufferLength = 0;
            int total=0;
            while ((bufferLength = inputStream.read(buffer)) != -1) {
            total += bufferLength;
            fileOutput.write(buffer, 0, bufferLength);

            asynch.publishProgress(bufferLength);

            }

非常感谢你

4

1 回答 1

1

ProgressBar在下载之间重置为零,因为该变量total也重置为零。

为了跟踪当前进度,进度条必须增加 ,progressBar.incrementProgressBy()而不是用 设置值progressBar.setProgress()

This means that the publishProgress() and onProgressUpdate() methods will also have to be modified to process increment instead of progress. And, the increment is your bufferLength:

asynch.publishProgress(bufferLength);

You will also have to initialize your ProgressBar with the total number of bytes for the sum of both downloads with progressBar.setMax().

于 2013-03-30T22:44:11.373 回答