2012-03-05 87 views
7

我真的很frustated now..I想下載一款用於文件和保存文件到SDCARD..and我得到了代碼:下載文件並將其保存到SD卡

 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錯誤=新的BufferedInputStream line..Pls幫助

+0

還請複製/粘貼錯誤和堆棧跟蹤。 – 2012-03-05 12:48:01

+0

我有複製從鏈接粘貼此代碼:http://stackoverflow.com/questions/6180926/android-dropbox-api-file-download...And在我的代碼它不認可「fd.is」 – Kanika 2012-03-05 12:49:33

+0

你是否確定'fd.is!= null'? – 2012-03-05 12:58:58

回答

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; 

Thanks all ..

+0

ParseCvs類在這裏做什麼? – 2013-11-22 11:39:17