问题
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