Это просто обходной путь. Создайте класс ProgressOutputStream
, расширив суперкласс OutputStream
.
public class ProgressOutputStream extends OutputStream {
public ProgressOutputStream(long totalFileSize, OutputStream stream, Listener listener) {
this.stream = stream;
this.listener = listener;
this.completed = 0;
this.totalFileSize = totalFileSize;
}
@Override
public void write(byte[] data, int off, int length) throws IOException {
this.stream.write(data, off, length);
track(length);
}
@Override
public void write(byte[] data) throws IOException {
this.stream.write(data);
track(data.length);
}
@Override
public void write(int c) {
this.stream.write(c);
track(1)
}
private void track(int length) {
this.completed += length;
this.listener.progress(this.completed, this.totalFileSize);
}
public interface Listener {
public void progress(long completed, long totalFileSize);
}
}
Звоните ProgressOutputStream
в вашем file.download()
как:
FileOutputStream stream = new FileOutputStream(info.getName());
file.download(new ProgressOutputStream(size, stream, new ProgressOutputStream.Listener() {
void progress(long completed, long totalFileSize) {
// update progress bar here ...
}
});
Дайте ему попытку. Надеюсь, что это даст представление.