使用Dropbox API通过Android上传文件 [英] Using Dropbox API to upload a file with Android

查看:188
本文介绍了使用Dropbox API通过Android上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Dropbox API通过Android将文件(图形,音频和视频文件)上传到Dropbox?我按照 Dropbox SDK Android 页上的教程进行操作,可以使示例工作.但是,现在我要上载一个实际的File对象而不是一个String并正在挣扎.

How can I upload a File (graphic, audio and video file) with Android using the Dropbox API to Dropbox? I followed the tutorial on the Dropbox SDK Android page and could get the sample to work. But now instead of a String I want to upload an actual File object and am struggling.

示例代码可以正常工作,并且看起来像这样:

The sample code works without any problems and looks like this:

    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.");
}   

但是当我尝试更改它并使用以下代码上传实际文件时:

But when I try to change it and upload an actual file with this code:

    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对象转换为字节流的某些操作一定是错误的,但这只是一个假设.

I have no success getting a DropboxException error. I think something where I try to convert the File object to the byte-stream must be wrong but this is just an assumption.

除了字符串"示例外,Android的"Dropbox"页面上没有其他文档.

Other than the String example there is nothing else documented on the Dropbox page for Android.

感谢您的帮助.

推荐答案

我找到了解决方案-如果有人对此感兴趣,那就是工作代码:

I found the solution - if anyone is interested here is the working code:

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.");
            }

这篇关于使用Dropbox API通过Android上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆