简体   繁体   English

android Dropbox同步api上传文件并显示进度条

[英]android Dropbox sync api upload file and display progress bar

My app look like the Dropbox Notes example from Dropbox sync sdk , except I want to display a progress bar in listview when uploading new file to Dropbox. 我的应用程序看起来像Dropbox sync sdk中的Dropbox Notes示例,但我想在将新文件上传到Dropbox时在列表视图中显示进度栏。
The example listen text file change to update file, but in my case, I upload many file types (text and binary). 该示例将侦听文本文件更改为更新文件,但就我而言,我上传了许多文件类型(文本和二进制)。 Here is my try to upload file: 这是我尝试上传的文件:

DbxPath path = new DbxPath("/test.pdf");
DbxAccount acct = NotesAppConfig.getAccountManager(getActivity()).getLinkedAccount();
DbxFile mFile;
DbxFileSystem fs = DbxFileSystem.forAccount(acct);
try {
   mFile = fs.open(path);
 } catch (DbxException.NotFound e) {
   mFile = fs.create(path);
   }
  mFile.addListener(mChangeListener);
  FileOutputStream out = mFile.getWriteStream();
  File sdcard = new File(Environment.getExternalStorageDirectory(), "test.pdf");
  FileInputStream in = new FileInputStream(sdcard);
  copyFile(in, out);
  out.close();
  in.close();

 private final DbxFile.Listener mChangeListener = new DbxFile.Listener() {

    @Override
    public void onFileChange(DbxFile file) {

        try {
            if(file.getSyncStatus().pending == PendingOperation.UPLOAD ){
                long byteTotal = file.getSyncStatus().bytesTotal;
                long byteTransfer = file.getSyncStatus().bytesTransferred;
                Log.d(TAG, "file changed: " +  byteTransfer + " / " + byteTotal);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
};

 public static void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while ((read = in.read(buffer)) != -1) {
        out.write(buffer, 0, read);
    }
}

The loader use this method to get file list: 加载程序使用此方法获取文件列表:

List<DbxFileInfo> entries = dbxFileSystem.listFolder(dbxPath); 

This code above can work, but it only displays file in listview when upload complete. 上面的代码可以使用,但是仅在上传完成后才在列表视图中显示文件。 Because the example use the DbxFileInfo , so I don't know how to get status of this file when it is uploading. 因为该示例使用DbxFileInfo ,所以我不知道如何在上传该文件时获取其状态。
Is there any way to do this? 有什么办法吗?

您可以为要上传的文件使用不同的标志,例如uploading,uploadup,Finish作为布尔变量

As soon as you save the file, I believe you should see it with listFolder . 保存文件后,我相信您应该使用listFolder看到它。 Are you saying that's not what you see? 您是说那不是您所看到的吗?

In terms of showing progress of an upload, keep in mind that the Sync API is designed to work offline with caching. 就显示上传进度而言,请记住,Sync API设计为可与缓存脱机工作。 It probably wouldn't make sense to show a progress bar if you're not connected to the internet... it could potentially be hours (or days) before the file is uploaded. 如果您没有连接到互联网,则显示进度条可能没有任何意义……文件上传可能需要数小时(或数天)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM