我的应用程序看起来像Dropbox sync sdk中的Dropbox Notes示例,只是我想在将新文件上传到Dropbox时在列表视图中显示进度条
示例侦听文本文件更改为更新文件,但在我的情况下,我上载了许多文件类型(文本和二进制)。这是我尝试上传的文件:
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);
}
}
加载程序使用此方法获取文件列表:
List<DbxFileInfo> entries = dbxFileSystem.listFolder(dbxPath);
上面的代码可以工作,但它只在上传完成时在列表视图中显示文件。因为这个例子使用了DbxFileInfo
,所以我不知道如何在上传时获得这个文件的状态
有办法做到这一点吗?
您可以对正在上传的文件的状态使用不同的标志,如上传、上传uploadFinish作为布尔变量
一旦保存了文件,我相信您应该在listFolder
中看到它。你是说你看到的不是那样吗?
在显示上传进度方面,请记住,Sync API设计为使用缓存脱机工作。如果你没有连接到互联网,显示进度条可能没有意义。。。上传文件可能需要数小时(或数天)。