2012-03-22 132 views
19

如何使用Dropbox API將Android上的文件(圖形,音頻和視頻文件)上傳到Dropbox?我按照Dropbox SDK Android頁面上的教程進行操作,並可以使樣本工作。但現在,而不是一個字符串我想上傳一個實際的文件對象,並掙扎。使用Dropbox API上傳帶有Android的文件

的示例代碼工作沒有任何問題,並且看起來是這樣的:

String fileContents = "Hello World!"; 
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileContents.getBytes()); 
try { 
    Entry newEntry = mDBApi.putFile("/testing_123456.txt", inputStream, fileContents.length(), null, null); 
} catch (DropboxUnlinkedException e) { 
    Log.e("DbExampleLog", "User has unlinked."); 
} catch (DropboxException e) { 
    Log.e("DbExampleLog", "Something went wrong while uploading."); 
} 

但是,當我試圖改變它,並上傳一個實際的文件與此代碼:

File tmpFile = new File(fullPath, "IMG_2012-03-12_10-22-09_thumb.jpg"); 

// convert File to byte[] 
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
ObjectOutputStream oos = new ObjectOutputStream(bos); 
oos.writeObject(tmpFile); 
bos.close(); 
oos.close(); 
byte[] bytes = bos.toByteArray(); 

ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); 
try { 
    Entry newEntry = mDBApi.putFile("/IMG_2012-03-12_10-22-09_thumb.jpg", inputStream, tmpFile.length(), null, null); 
} catch (DropboxUnlinkedException e) { 
    Log.e("DbExampleLog", "User has unlinked."); 
} catch (DropboxException e) { 
    Log.e("DbExampleLog", "Something went wrong while uploading."); 
} 

我沒有成功得到一個DropboxException錯誤。我想我試圖將File對象轉換爲字節流的東西一定是錯的,但這只是一個假設。

除了字符串示例之外,Android的Dropbox頁面上沒有其他記錄。

感謝您的任何幫助。

回答

23

我找到了解決辦法 - 如果有人有興趣這裏是工作代碼:

private DropboxAPI<AndroidAuthSession> mDBApi;//global variable 

File tmpFile = new File(fullPath, "IMG_2012-03-12_10-22-09_thumb.jpg"); 

FileInputStream fis = new FileInputStream(tmpFile); 

      try { 
       DropboxAPI.Entry newEntry = mDBApi.putFileOverwrite("IMG_2012-03-12_10-22-09_thumb.jpg", fis, tmpFile.length(), null); 
      } catch (DropboxUnlinkedException e) { 
       Log.e("DbExampleLog", "User has unlinked."); 
      } catch (DropboxException e) { 
       Log.e("DbExampleLog", "Something went wrong while uploading."); 
      } 
+7

是什麼mDBApi在此代碼? – TharakaNirmana 2013-01-22 11:49:17

+1

我知道這個答案很晚,但誰知道這可能會拯救一些人。它是一個全局變量。這是你應該添加的代碼: private DropboxAPI mDBApi; – Yenthe 2013-10-24 13:51:17

+0

private DropboxAPI mDBApi; – nikki 2013-11-16 09:38:23

2

@ E-自然的答案是正確的相比更多...只是覺得應該指出大家的Dropbox的官方網站,顯示如何upload a file and much more

此外,@ e-nature的答案會覆蓋具有相同名稱的文件,因此如果您不想要該行爲,只需使用.putFile而不是.putFileOverwrite.putFile有一個額外的參數,您可以簡單地將null添加到最後。 More info

5

這裏是的Dropbox API的另一個實施上傳下載的文件。 這可以用於任何類型的文件。

String file_name = "/my_file.txt"; 
String file_path = Environment.getExternalStorageDirectory() 
     .getAbsolutePath() + file_name; 
AndroidAuthSession session; 

public void initDropBox() { 

    AppKeyPair appKeys = new AppKeyPair(APP_KEY, APP_SECRET); 
    session = new AndroidAuthSession(appKeys); 
    mDBApi = new DropboxAPI<AndroidAuthSession>(session); 
    mDBApi.getSession().startOAuth2Authentication(MyActivity.this); 

} 

Entry response; 

public void uploadFile() { 
    writeFileContent(file_path); 
    File file = new File(file_path); 
    FileInputStream inputStream = null; 
    try { 
     inputStream = new FileInputStream(file); 
    } catch (FileNotFoundException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
    } 


    try { 
     response = mDBApi.putFile("/my_file.txt", inputStream, 
       file.length(), null, null); 
     Log.i("DbExampleLog", "The uploaded file's rev is: " + response.rev); 
    } catch (DropboxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 

    } 

} 
public void downloadFile() { 

    File file = new File(file_path); 
    FileOutputStream outputStream = null; 

    try { 
     outputStream = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    DropboxFileInfo info = null; 
    try { 
     info = mDBApi.getFile("/my_file.txt", null, outputStream, null); 



     Log.i("DbExampleLog", "The file's rev is: " 
       + info.getMetadata().rev); 
    } catch (DropboxException e) { 
     // TODO Auto-generated catch block 

     e.printStackTrace(); 
    } 

} 

@Override 
    protected void onResume() { 
     // TODO Auto-generated method stub 
     super.onResume(); 
     if (mDBApi.getSession().authenticationSuccessful()) { 
      try { 
       // Required to complete auth, sets the access token on the 
       // session 

      mDBApi.getSession().finishAuthentication(); 

      String accessToken = mDBApi.getSession().getOAuth2AccessToken(); 

      /** 
      * You'll need this token again after your app closes, so it's 
      * important to save it for future access (though it's not shown 
      * here). If you don't, the user will have to re-authenticate 
      * every time they use your app. A common way to implement 
      * storing keys is through Android's SharedPreferences API. 
      */ 

     } catch (IllegalStateException e) { 
      Log.i("DbAuthLog", "Error authenticating", e); 
     } 
    } 
} 

- >通話uploadFile()和downLoadFile()的子線程的方法,否則它會給你一個例外

- >對於使用的AsyncTask和調用這些上面doInBackground方法方法。

希望這將有助於...謝謝

2

下面是使用Dropbox的V2 API,但第三方SDK另一個例子。順便說一下,Google Drive,OneDrive和Box.com的工作原理完全相同。

// CloudStorage cs = new Box(context, "[clientIdentifier]", "[clientSecret]"); 
// CloudStorage cs = new OneDrive(context, "[clientIdentifier]", "[clientSecret]"); 
// CloudStorage cs = new GoogleDrive(context, "[clientIdentifier]", "[clientSecret]"); 
CloudStorage cs = new Dropbox(context, "[clientIdentifier]", "[clientSecret]"); 
new Thread() { 
    @Override 
    public void run() { 
     cs.createFolder("/TestFolder"); // <--- 
     InputStream stream = null; 
     try { 
      AssetManager assetManager = getAssets(); 
      stream = assetManager.open("UserData.csv"); 
      long size = assetManager.openFd("UserData.csv").getLength(); 
      cs.upload("/TestFolder/Data.csv", stream, size, false); // <--- 
     } catch (Exception e) { 
      // TODO: handle error 
     } finally { 
      // TODO: close stream 
     } 
    } 
}.start(); 

它使用CloudRail Android SDK