2011-05-30 123 views
0

我目前正在開發一款支持android dropbox api的android應用程序。我已經得到它的工作,以便它從android SD卡上的文件發送到Dropbox文件夾。然後,我稍後需要能夠下載此文件並再次將其保存到手機SD卡。Android Dropbox API文件下載

如何從Dropbox下載文件並將其保存到設備中,但很少或根本沒有關於android api的文檔。

感謝您提供的任何幫助。

+0

從您的API變量中使用api.putFile(「dropbox」,「/ location/on/dropbox」,路徑/ on/sdcard);希望這有助於 – Boardy 2011-09-09 22:56:39

+0

多一個懷疑,我應該如何使consumer_key和consumer_secret對所有用戶都是唯一的,因爲在我的應用程序中顯示網絡錯誤的另一個用戶登錄?爲什麼如此 – Abhi 2011-09-10 04:45:21

+0

從我的理解來看,我相信當你登錄到DropBox時,DropBox服務器返回消費者密鑰和消費者密鑰,所以它應該對嘗試登錄到DropBox的每個用戶都是唯一的,網絡錯誤可能是其他內容。我確實發現,在調試時,它會隨機停止工作,說它無法連接,但離開後它會工作。想想Dropbox可能會阻止你登錄過多次,所以這可能是你的問題 – Boardy 2011-09-10 23:05:45

回答

4
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; 
} 

來源:http://forums.dropbox.com/topic.php?id=23189&replies=5#post-159521

0
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; 
} 

這將可能爲你工作。