1

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?

ductran
  • 10,043
  • 19
  • 82
  • 165
  • Did you solve this problem ? – Salman Khakwani Dec 09 '13 at 11:19
  • Unfortunately, no. I gave up and change to `core api`, which is easy to use and control what I want. I also posted this question into dropbox forum but nobody answered me: https://forums.dropbox.com/topic.php?id=108295 – ductran Dec 09 '13 at 15:36

2 Answers2

0

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

0

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.

user94559
  • 59,196
  • 6
  • 103
  • 103
  • Sorry, the file can add into list and it can upload in background. The problem is it don't notify the listener with progress update. Therefore, my progress bar can't display the progress correctly. First, the progress bar display 0%, and it wait for file upload complete then set 100% without any middle progress value. – ductran Dec 13 '13 at 13:36
  • I don't think what you want is really possible with the Sync API. – user94559 Dec 13 '13 at 19:30
  • Are you sure? so the listener is used for what? – ductran Dec 14 '13 at 05:49
  • Which listener? The listener on a file is used to notify you when the file has changed remotely (and when that change has been downloaded so you can use the new version). – user94559 Dec 14 '13 at 17:24
  • Ok, so the answer is I should use `core api` instead of `sync api`. – ductran Dec 14 '13 at 18:20
  • If you really need a progress bar during upload, then I suppose so. But what does your app do when it's offline? – user94559 Dec 14 '13 at 18:49
  • If there is no connection, it just keep the file in list and don't do anything (display the status not upload yet). But whenever it starts uploading, it must tell user know by using progress bar. – ductran Dec 15 '13 at 04:29
  • That's going to be fairly difficult to build yourself... handling being offline is a big reason people use the Sync API. – user94559 Dec 15 '13 at 04:34