使用Android中的异步任务从Dropbox文件夹下载一个文件并保存到SD卡中不工作
1.我想从Android中的Dropbox文件夹下载一个特定文件,然后保存到本地SD卡中。 2.我参考了DbRoulete示例,它从Dropbox下载随机图片 另外,我尝试了下面的代码,但没有得到任何下载文件的解决方案 请任何人帮帮我 -此代码随机下载图像使用Android中的异步任务从Dropbox文件夹下载一个文件并保存到SD卡中不工作,android,download,android-asynctask,dropbox,Android,Download,Android Asynctask,Dropbox,1.我想从Android中的Dropbox文件夹下载一个特定文件,然后保存到本地SD卡中。 2.我参考了DbRoulete示例,它从Dropbox下载随机图片 另外,我尝试了下面的代码,但没有得到任何下载文件的解决方案 请任何人帮帮我 -此代码随机下载图像 private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException{ BufferedInputStream br = null
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;
}
private boolean downloadDropboxFile(字符串dbPath,文件localFile)引发IOException{
BufferedInputStream br=null;
BufferedOutputStream bw=null;
试一试{
如果(!localFile.exists()){
localFile.createNewFile();//否则dropbox客户端将以静默方式失败
}
FileDownload fd=api.getFileStream(“dropbox”,dbPath,null);
br=新的BufferedInputStream(fd.is);
bw=新的BufferedOutputStream(新的FileOutputStream(localFile));
字节[]缓冲区=新字节[4096];
int-read;
while(true){
read=br.read(缓冲区);
如果(read),您可以使用以下两种方法将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"/>
在项目中插入dropboxchooser sdk并使用此代码。这将打开dropbox对话框并查看文件列表。所选文件将下载到手机存储器中
Dropbox Android Core SDK提供两种下载文件内容的方法。文档链接如下:
在这两种情况下,第一个参数都是Dropbox中要下载的文件的路径。DbRoulete示例应用程序随机选择文件,但在实际使用中,您将提供特定路径,例如,用户选择的特定文件的路径。例如,您可以使用以下任一方法获取有关哪些文件可用的信息:
Dropbox Android Core SDK教程还有一个小示例:
在该示例代码中,正在访问位于/magnum-opus.txt的Dropbox中的一个文件。但没有获得任何解决方案。
???您想获得一个文件,不是吗?您得到了什么?请说明如何调用downloadDropboxFile();查看BDRoulete示例,应该会得到正确的代码。我在DbRoutle中获得随机图片下载代码未获得指定文件。我想从dropbox下载一个特定文件,然后保存到Sdcard文件夹。你能帮我@greenappsi吗?我不想选择任何内容。只需要向dropbox发送请求,用filenam下载一个文件e我必须从Dropbox中获取图像文件并保存到SDCard中。请编辑此代码,我无法下载该文件。请澄清我,下载该文件的方法,以及如何将下载文件保存到SDCard中。file file file=new file(“/magnum opus.txt”);FileOutputStream outputStream=new FileOutputStream(file);DropboxFileInfo=mDBApi.getFile(“/magnum opus.txt”,null,outputStream,null);Log.i(“DbExampleLog”,“文件的版本是:”+info.getMetadata().rev);这段代码我得到的文件找不到异常,请帮助我。链接的Dropbox中是否有文件“/magnum opus.txt”?我已尝试使用不同的文件,但我发现文件未找到异常
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);
}