Android Dropbox API文件下载

时间:2011-05-30 21:31:56

标签: android dropbox-api

我目前正在开发一款支持android dropbox api的Android应用程序。我有它的工作,以便它从android SD卡发送一个文件到一个Dropbox文件夹。然后,我需要能够下载此文件并再次将其保存到手机SD卡。

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

感谢您提供的任何帮助。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

如果尝试此链接,Download file from Dropbox and save it into SDCARD真的有助于通过投递箱下载任何类型的文件

答案 2 :(得分: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;
}

它可能适合你。