从Dropbox下载文件并保存到SD卡中

7

我现在感到非常沮丧..我想从Dropbox下载文件并将该文件保存到SDCARD中..我得到的代码如下:

     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;

这里在br=new BufferedInputStream一行出现了错误,请帮忙解决。


还请复制/粘贴错误和堆栈跟踪信息。 - Alex Florescu
我从链接http://stackoverflow.com/questions/6180926/android-dropbox-api-file-download复制粘贴了这段代码...但在我的代码中,它无法识别“fd.is”。 - Kanika
你确定 fd.is != null 吗? - Andrey Starodubtsev
你知道从Dropbox下载文件的其他方法吗?因为我已经复制粘贴了那段代码,但它并没有起作用。 - Kanika
1个回答

13

我找到了方法:

      File file= new File("/sdcard/New_csv_file.csv");
      OutputStream out= null;
      boolean result=false;
      try {
            out = new BufferedOutputStream(new FileOutputStream(file));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
               DropboxFileInfo info = mApi.getFile("/photos/New_csv_file.csv", null, out, null);
               Log.i("DbExampleLog", "The file's rev is: " + info.getMetadata().rev);
               Intent JumpToParseCSV=new Intent(context,ParseCSV.class);
                JumpToParseCSV.putExtra("FileName", file.getAbsolutePath());
                Log.i("path", "FileName"+ file.getAbsolutePath());
                 ((Activity) context).finish();
                context.startActivity(JumpToParseCSV);
                result=true;
            } catch (DropboxException e) {
               Log.e("DbExampleLog", "Something went wrong while downloading.");
               file.delete();
               result=false;
            }


    return result;

感谢大家...


1
这里的ParseCvs类是做什么的? - Hassaan Rabbani

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接