Dropbox APIを使用してAndroidでファイル(グラフィック、オーディオ、ビデオファイル)をDropboxにアップロードするにはどうすればよいですか?Dropbox SDK Androidページのチュートリアルに従い、サンプルを動作させることができました。しかし、文字列の代わりに、実際のFileオブジェクトをアップロードしたいので、苦労しています。
サンプルコードは問題なく動作し、次のようになります。
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ページに記載されているものはありません。
助けてくれてありがとう。