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?