android Dropbox sync api upload file and display progress bar

微笑、不失礼 提交于 2019-12-08 07:40:44

问题


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.
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.
Is there any way to do this?


回答1:


You can use different flags for the status of the file which is being uploaded, like uploading,uploaded uploadFinish as boolean varibles




回答2:


As soon as you save the file, I believe you should see it with 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. 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.



来源:https://stackoverflow.com/questions/20100698/android-dropbox-sync-api-upload-file-and-display-progress-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!