如何使用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頁面上沒有其他記錄。
感謝您的任何幫助。
是什麼mDBApi在此代碼? – TharakaNirmana 2013-01-22 11:49:17
我知道這個答案很晚,但誰知道這可能會拯救一些人。它是一個全局變量。這是你應該添加的代碼: private DropboxAPI mDBApi; –
Yenthe
2013-10-24 13:51:17
private DropboxAPI mDBApi; –
nikki
2013-11-16 09:38:23