繁体   English   中英

Android Dropbox API 文件下载

[英]Android Dropbox API file download

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

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

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

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

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

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;
}

它可能对你有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM