private FileOutputStream out;
private String packageName = "recitewords.apj.com.recitewords";
private String path = "/data/data/" + packageName + "/databases/" + "ReciteWords_0.db";
//定义一个字节输出流、文件路径名即下载到哪
new Thread(new Runnable() {
@Override
public void run() {
Map map = new HashMap();
AVFile avFile = new AVFile("ReciteWords_0.db", "https://dn-2NCdWBgh.qbox.me/bdd7cd3de40bae640f.db", map);
avFile.getDataInBackground(new GetDataCallback() {
@Override
public void done(byte[] bytes, AVException e) {
Toast.makeText(mActivity, "e:"+ e, Toast.LENGTH_SHORT).show();
if (e == null) {
File file = new File(path);
try {
out = new FileOutputStream(file);
out.write(bytes);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}finally {
try {
out.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
}, new ProgressCallback() {
@Override
public void done(final Integer integer) {
Toast.makeText(mActivity, "进度:"+integer, Toast.LENGTH_SHORT).show();
}
});
}
}).start();
上述例子。将文件放到leancloud的file中。导入leancloud4个包后,在application初始化。 创建AVFile对象传入文件名、leancloud文件路径、HashMap。
调用AVFile的getDataInBackground方法传入一个获取数据回调、下载进度回调。其中数据回调中的bytes既是文件字节流,将文件字节流通过FileOutputStream输出到文件即可完成下载。