1.我想在Android中从Dropbox文件夹下载特定文件,然后保存到本地SD卡。
2.我提到了DBRoulette示例,它从Dropbox下载随机图片。
另外,我尝试了以下代码,但没有获取任何解决方案来下载文件。
请有人帮助我。
Link I referred - 此代码随机下载图片。
private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException{
BufferedInputStream br = null;
BufferedOutputStream bw = null;
try {
if (!localFile.exists()) {
localFile.createNewFile(); //otherwise dropbox client will fail silently
}
FileDownload fd = api.getFileStream("dropbox", dbPath, null);
br = new BufferedInputStream(fd.is);
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);
}
} finally {
//in finally block:
if (bw != null) {
bw.close();
}
if (br != null) {
br.close();
}
}
return true;
}
答案 0 :(得分:1)
您可以使用以下两种方法将dropbox目录下的文件复制到新文件中。
public static final void copyDirectory(File dropboxfile, File newfile) throws IOException {
newfile.mkdirs();
File[] files = dropboxfile.listFiles();
for (File file : files) {
if (file.isDirectory()) {
copyDirectory(file, new File(newfile, file.getName()));
} else {
copyFile(file, new File(newfile, file.getName()));
}
}
}
public static final void copyFile(File source, File destination) throws IOException {
FileChannel sourceChannel = new FileInputStream(source).getChannel();
FileChannel targetChannel = new FileOutputStream(destination).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
sourceChannel.close();
targetChannel.close();
}
您可以获得如下的Dropbox文件:
File dropboxfile = new File(root_to_dropbox_file, file_name);
sd卡下的新文件可以创建如下:
String root = Environment.getExternalStorageDirectory().toString();
File newfile = new File(root + "/selected_name");
最后但并非最不重要的是将以下权限添加到您的Android清单文件中:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<android:uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
答案 1 :(得分:1)
Dropbox Android Core SDK提供了两种下载文件内容的方法。文档链接如下:
在两者中,第一个参数是Dropbox中要下载的文件的路径。 DBRoulette示例应用程序随机选择文件,但在实际使用情况下,您将提供特定路径,例如,用户选择的特定文件。例如,您可以使用以下任一方式获取有关可用文件的信息:
Dropbox Android Core SDK教程也有一个小样本:
https://www.dropbox.com/developers/core/start/android#downloading
在该示例代码中,正在访问位于/magnum-opus.txt的Dropbox中的文件。
答案 2 :(得分:0)
在项目中插入dropboxchooser sdk并使用此代码。这将打开Dropbox对话框并查看文件list.selected文件将下载到您的手机存储中。
private DbxChooser mChooser;
private final int DBX_CHOOSER_REQUEST = 2020;
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
setContentView(R.layout.activity_main);
mChooser.forResultType(DbxChooser.ResultType.FILE_CONTENT)
.launch(DropBoxDemo.this, DBX_CHOOSER_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == DBX_CHOOSER_REQUEST)
{
if(resultCode==RESULT_OK)
{
DbxChooser.Result result = new DbxChooser.Result(data);
Log.d("dropbox", "Link to selected file: " + result.getLink());
Log.d("dropbox", "Icon to selected file: " + result.getIcon());
Log.d("dropbox", "Name to selected file: " + result.getName());
Log.d("dropbox", "Size to selected file: " + result.getSize());
}
}
super.onActivityResult(requestCode, resultCode, data);
}