2011-05-30 139 views
0

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

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

感谢您提供的任何帮助。

+0

从您的API变量中使用api.putFile(“dropbox”,“/ location/on/dropbox”,路径/ on/sdcard);希望这有助于 – Boardy 2011-09-09 22:56:39

+0

多一个怀疑,我应该如何使consumer_key和consumer_secret对所有用户都是唯一的,因为在我的应用程序中显示网络错误的另一个用户登录?为什么如此 – Abhi 2011-09-10 04:45:21

+0

从我的理解来看,我相信当你登录到DropBox时,DropBox服务器返回消费者密钥和消费者密钥,所以它应该对尝试登录到DropBox的每个用户都是唯一的,网络错误可能是其他内容。我确实发现,在调试时,它会随机停止工作,说它无法连接,但离开后它会工作。想想Dropbox可能会阻止你登录过多次,所以这可能是你的问题 – Boardy 2011-09-10 23:05:45

回答

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

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

这将可能为你工作。