我正在尝试使用Box API应用程序的下载功能从Box API应用程序下载文件,就像here所说的那样。
...
FileOutputStream stream = new FileOutputStream(info.getName());
// Provide a ProgressListener to monitor the progress of the download.
file.download(stream, new ProgressListener() {
public void onProgressChanged(long numBytes, long totalBytes) {
double percentComplete = numBytes / totalBytes;
}
});
....但是,我无法使用onProgessChanged函数。有没有关于如何访问它的例子?我如何访问它?
发布于 2019-03-21 13:05:23
这只是一种解决方法。通过扩展超类OutputStream创建一个ProgressOutputStream类。
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);
}
}在file.download()中调用ProgressOutputStream,如下所示:
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 ...
}
});试试看。希望这能给出一个想法。
https://stackoverflow.com/questions/55273882
复制相似问题