android – 从Dropbox下载文件并将其保存到SDCARD

前端之家收集整理的这篇文章主要介绍了android – 从Dropbox下载文件并将其保存到SDCARD前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我现在真的很沮丧..我想从DropBox下载文件并将该文件保存到SDCARD ..我得到的代码为:
  1. private boolean downloadDropBoxFile(String dbPath,File localFile) throws IOException{
  2.  
  3. BufferedInputStream br = null;
  4. BufferedOutputStream bw = null;
  5.  
  6. try {
  7. if (!localFile.exists()) {
  8. localFile.createNewFile(); //otherwise dropBox client will fail silently
  9. }
  10.  
  11. FileDownload fd = api.getFileStream("dropBox",dbPath,null);
  12. **br = new BufferedInputStream(fd.is);**
  13. bw = new BufferedOutputStream(new FileOutputStream(localFile));
  14.  
  15. byte[] buffer = new byte[4096];
  16. int read;
  17. while (true) {
  18. read = br.read(buffer);
  19. if (read <= 0) {
  20. break;
  21. }
  22. bw.write(buffer,read);
  23. }
  24. } finally {
  25. //in finally block:
  26. if (bw != null) {
  27. bw.close();
  28. }
  29. if (br != null) {
  30. br.close();
  31. }
  32. }
  33.  
  34. return true;

}

这里我在br = new BufferedInputStream line..Pls帮助中收到错误

解决方法

我找到了方法
  1. File file= new File("/sdcard/New_csv_file.csv");
  2. OutputStream out= null;
  3. boolean result=false;
  4. try {
  5. out = new BufferedOutputStream(new FileOutputStream(file));
  6. } catch (FileNotFoundException e1) {
  7. // TODO Auto-generated catch block
  8. e1.printStackTrace();
  9. }
  10. try {
  11. DropBoxFileInfo info = mApi.getFile("/photos/New_csv_file.csv",null,out,null);
  12. Log.i("DbExampleLog","The file's rev is: " + info.getMetadata().rev);
  13. Intent JumpToParseCSV=new Intent(context,ParseCSV.class);
  14. JumpToParseCSV.putExtra("FileName",file.getAbsolutePath());
  15. Log.i("path","FileName"+ file.getAbsolutePath());
  16. ((Activity) context).finish();
  17. context.startActivity(JumpToParseCSV);
  18. result=true;
  19. } catch (DropBoxException e) {
  20. Log.e("DbExampleLog","Something went wrong while downloading.");
  21. file.delete();
  22. result=false;
  23. }
  24.  
  25.  
  26. return result;

谢谢大家….

猜你在找的Android相关文章