首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >use box-api进度监听器

use box-api进度监听器
EN

Stack Overflow用户
提问于 2019-03-21 12:38:04
回答 1查看 74关注 0票数 0

我正在尝试使用Box API应用程序的下载功能从Box API应用程序下载文件,就像here所说的那样。

代码语言:javascript
复制
...
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函数。有没有关于如何访问它的例子?我如何访问它?

EN

回答 1

Stack Overflow用户

发布于 2019-03-21 13:05:23

这只是一种解决方法。通过扩展超类OutputStream创建一个ProgressOutputStream类。

代码语言:javascript
复制
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,如下所示:

代码语言:javascript
复制
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 ...
    }
});

试试看。希望这能给出一个想法。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55273882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档