Dropbox的Android的API同步上传文件,并显示进度条 [英] android Dropbox sync api upload file and display progress bar
问题描述
我的应用程序看起来像的 Dropbox的同步SDK ,但我要上传新文件,Dropbox的时候在列表视图中显示进度条。结果这个例子听文本文件的变化更新文件,但对我来说,我上传多个文件类型(文本和二进制)。这是我尝试上传文件:
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);
这code以上即可工作,但只有当上传完成显示文件列表视图。因为该示例使用 DbxFileInfo
,所以我不知道如何获取该文件的状态,当它被上传。结果有没有办法做到这一点?
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?
推荐答案
只要你保存文件,我相信你应该用 listFolder
看到它。你说,这不是你看到了什么?
As soon as you save the file, I believe you should see it with listFolder
. Are you saying that's not what you see?
在显示上传进度而言,记住同步API旨在与缓存脱机工作。它可能没有什么意义,显示一个进度条,如果你没有连接到互联网......这可能是小时(或几天)文件上传之前。
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.
这篇关于Dropbox的Android的API同步上传文件,并显示进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!