Download file from Dropbox and save it into SDCARD

你离开我真会死。 提交于 2020-01-01 09:27:10

问题


Am really frustated now..I want to download a file from Dropbox and save that file into SDCARD..and I got the code as:

     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;

}

Here I am getting an error on br=new BufferedInputStream line..Pls help


回答1:


I found the way:

      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;

Thanks all....



来源:https://stackoverflow.com/questions/9566884/download-file-from-dropbox-and-save-it-into-sdcard

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!