gpt4 book ai didi

android - 从 Dropbox 下载所有类型的文件

转载 作者:行者123 更新时间:2023-11-29 02:04:10 27 4
gpt4 key购买 nike

我正在研究 Dropbox。我看到了文档。这是我显示列表的代码:

Entry entryCheck = mApi.metadata("/", 100, null, true, null);
Log.i("Item Name", entryCheck.fileName());
Log.i("Is Folder", String.valueOf(entryCheck.isDir));

我从 dropbox 得到了所有列表,但我的问题是

  1. 这里 entryCheck.isDir 总是给我真值,如果它是文件或目录,那么我怎么知道哪个是文件或哪个是目录?
  2. 我是如何下载这些文件的。

我试过这个但它不工作:

private boolean downloadDropboxFile(String dbPath, File localFile,
DropboxAPI<?> api) throws IOException {
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
if (!localFile.exists()) {
localFile.createNewFile(); // otherwise dropbox client will fail
// silently
}

DropboxInputStream fin = mApi.getFileStream("dropbox", dbPath);
br = new BufferedInputStream(fin);
bw = new BufferedOutputStream(new FileOutputStream(localFile));

byte[] buffer = new byte[4096];
int read;
while (true) {
read = br.read(buffer);
if (read <= 0) {
break;
}
bw.write(buffer, 0, read);
}
} catch (DropboxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// in finally block:
if (bw != null) {
bw.close();
}
if (br != null) {
br.close();
}
}

return true;
}

最佳答案

这会起作用

String inPath ="mnt/sdcard/"+filename;
File file=new File(inPath);
try {

mFos = new FileOutputStream(file);
} catch (FileNotFoundException e) {
mErrorMsg = "Couldn't create a local file to store the image";
return false;
}

mApi.getFile("/"+filename, null, mFos, null);

这将下载文件并将其存储在 SD 卡位置 inPath 中。您必须在新线程中执行此操作,而不是在主线程中执行。使用 AsyncTask。 http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html此链接解释了原因..

关于android - 从 Dropbox 下载所有类型的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10943438/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com