2013-08-19 39 views
1

到目前爲止,我已經成功地將Dropbox API與我的項目集成在一起。爲此,我一直在使用Dropbox SDK中給出的示例程序。從我可以下載的方式(隨機圖片)和輕鬆上傳文件。我的問題是,我們如何從Dropbox帳戶一次下載一個文件夾或多個文件? 。此外,當我點擊下載按鈕,它隨機選擇一個圖像文件,然後顯示,而不是這樣做,我想下載所有圖像文件或特定的文件夾。任何建議或幫助,將不勝感激。從android的下拉框中下載多個文件或文件夾?

在此先感謝。

+0

可能重複檢索的所有文件和文件夾: http://stackoverflow.com/questions/6180926/android- dropbox-api-file-download –

回答

0

Sync API的工作原理是在打開它們時按需下載文件。因此,只需打開您正在嘗試閱讀的所有文件。

+0

如何做到這一點?我們只能逐個傳遞文件名。你的意思是遞歸地做這個?另一方面,如果用戶不知道文件名意味着如何能夠只下載使用路徑。 – chain

+0

您可以列出該路徑中的文件並根據需要打開它們。 – smarx

+0

可否請您發佈一段代碼。這對我真的很有幫助。 – chain

1

嗨,請通過下面的代碼,這可能對你有幫助。

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

欲瞭解更多信息,請查看source

+0

在接受你的答案之前,我想先清楚地理解代碼。通過使用此代碼,我們可以下載單個文件或整個文件夾或所提及路徑上的所有可用文件? FileDownload類是在Dropbox API中預定義的,或者我們必須手動創建它?我知道這可能是一個愚蠢的問題,即使我應該清楚我的疑惑。 – chain

+0

這看起來不相關。 (我猜這是使用Core API而不是Sync API?) – smarx

1

如果您需要從下拉框中獲取所有文件和文件夾,只需將此代碼複製並粘貼到項目中並查看結果即可。它會幫助你很多。

String mPath="/"; //You can change the path here to specific FOLDER 
Entry dirent = null; 
try 
    { 
     dirent = mApi.metadata(mPath, 1000, null, true, null);    
    } 
catch (DropboxException e) 
    { 
    System.out.println("Error Detail "+e.getMessage()); 
    } 

//執行一個循環,並從PATH

 for (Entry ent: dirent.contents) 
    { 
     String name = ent.fileName();       
     System.out.println("My File in Folder "+name);       
     }