我该如何使用Dropbox API将文件(图形、音频和视频文件)上传到Dropbox?我按照Dropbox SDK 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错误。我认为在尝试将文件对象转换为字节流时出了些问题,但这只是一种假设。
除了String示例之外,在Dropbox Android页面上没有其他文档。
感谢任何帮助。