[英]android Dropbox sync api upload file and display progress bar
我的应用程序看起来像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
,所以我不知道如何在上传该文件时获取其状态。
有什么办法吗?
您可以为要上传的文件使用不同的标志,例如uploading,uploadup,Finish作为布尔变量
保存文件后,我相信您应该使用listFolder
看到它。 您是说那不是您所看到的吗?
就显示上传进度而言,请记住,Sync API设计为可与缓存脱机工作。 如果您没有连接到互联网,则显示进度条可能没有任何意义……文件上传可能需要数小时(或数天)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.