2012-06-08 36 views
2

我正在使用Dropbox。我看到了文檔。這是我的代碼顯示列表:從Dropbox下載所有類型的文件

Entry entryCheck = mApi.metadata("/", 100, null, true, null); 
Log.i("Item Name", entryCheck.fileName()); 
Log.i("Is Folder", String.valueOf(entryCheck.isDir)); 

我從Dropbox的所有清單,但我的問題是,

  1. 這裏entryCheck.isDir總是給我真正的價值,如果它是文件或目錄,以便我如何可以知道哪個文件或哪個目錄?
  2. 我是如何下載這些文件的。

我試着用這一點,但它不工作:

private boolean downloadDropboxFile(String dbPath, File localFile, 
     DropboxAPI<?> api) throws IOException { 
    BufferedInputStream br = null; 
    BufferedOutputStream bw = null; 
    try { 
     if (!localFile.exists()) { 
      localFile.createNewFile(); // otherwise dropbox client will fail 
      // silently 
     }     

     DropboxInputStream fin = mApi.getFileStream("dropbox", dbPath); 
     br = new BufferedInputStream(fin); 
     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); 
     } 
    } catch (DropboxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally { 
     // in finally block: 
     if (bw != null) { 
      bw.close(); 
     } 
     if (br != null) { 
      br.close(); 
     } 
    } 

    return true; 
} 

回答

1

這將工作

String inPath ="mnt/sdcard/"+filename; 
File file=new File(inPath);   
try { 

    mFos = new FileOutputStream(file); 
} catch (FileNotFoundException e) { 
    mErrorMsg = "Couldn't create a local file to store the image"; 
    return false; 
} 

mApi.getFile("/"+filename, null, mFos, null); 

此下載文件並將其存儲在SD卡的位置inPath。 你必須在新線程中執行,而不是在主線程中。使用AsyncTask。 http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html 此鏈接解釋了爲什麼..