Reputation: 39
i want to download multiple file(2 for the moment) in one progress bar(without reseting the progress bar)
In order to do this , i retrieve the total lenght of all file in order to use it in the publish progress method
The problem is that my progress bar reset to 0 once reached 50% then increment again until another 50%
Here is my code
@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]);
}
The code of my download function where i call the publishprogress method
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);
}
Thank you very much
Upvotes: 1
Views: 778
Reputation: 12656
Your ProgressBar
resets to zero between downloads because the variable total
also resets to zero.
In order to keep track of the current progress, the progress bar will have to increment with progressBar.incrementProgressBy()
instead of setting the value with 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()
.
Upvotes: 1