0

我目前正在开发一个支持 android dropbox api 的 android 应用程序。我已经让它工作了,它可以将一个文件从 android sd 卡发送到一个保管箱文件夹。然后我以后需要能够下载此文件并将其再次保存到手机的 SD 卡中。

如何从 Dropbox 下载文件并将其保存到设备,关于 android api 的文档很少甚至没有。

感谢您的任何帮助,您可以提供。

4

3 回答 3

4
private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException{

    BufferedInputStream br = null;
    BufferedOutputStream bw = null;

    try {
        if (!localFile.exists()) {
            localFile.createNewFile(); //otherwise dropbox client will fail silently
        }

        FileDownload fd = api.getFileStream("dropbox", dbPath, null);
        br = new BufferedInputStream(fd.is);
        bw = new BufferedOutputStream(new FileOutputStream(localFile));

        byte[] buffer = new byte[4096];
        int read;
        while (true) {
        read = br.read(buffer);
        if (read <= 0) {
        break;
        }
        bw.write(buffer, 0, read);
        }
    } finally {
        //in finally block:
        if (bw != null) {
            bw.close();
        }
        if (br != null) {
            br.close();
        }
    }

    return true;
}

来源:http ://forums.dropbox.com/topic.php?id=23189&replies=5#post-159521

于 2011-05-30T21:39:14.367 回答
0

如果尝试此链接,从 Dropbox 下载文件并将其保存到 SDCARD 确实有助于通过下拉框下载任何类型的文件

于 2013-07-05T09:21:18.850 回答
0
private boolean downloadDropboxFile(String dbPath, File localFile) throws IOException{

    BufferedInputStream br = null;
    BufferedOutputStream bw = null;

    try {
        if (!localFile.exists()) {
            localFile.createNewFile(); //otherwise dropbox client will fail silently
        }

        FileDownload fd = api.getFileStream("dropbox", dbPath, null);
        br = new BufferedInputStream(fd.is);
        bw = new BufferedOutputStream(new FileOutputStream(localFile));

        byte[] buffer = new byte[4096];
        int read;
        while (true) {
        read = br.read(buffer);
        if (read <= 0) {
        break;
        }
        bw.write(buffer, 0, read);
        }
    } finally {
        //in finally block:
        if (bw != null) {
            bw.close();
        }
        if (br != null) {
            br.close();
        }
    }

    return true;
}

它可能对你有用。

于 2015-08-06T05:44:32.823 回答