我们的应用是一款类似 wallpaper的应用,里面都是大量图片,所以把图片放到了服务器上,但是在app中,使用Laya.loader.load 加载的资源它并不会缓存到本地,每次都要重新下载,有没有什么api是可以手动缓存到本地的?
当前我自己写了一个下载方法,虽然文件保存成功了,但是用Laya.loader.load 读取的时候始终读不了
private function downloadFile(url:String,onComplete:Handler,onError:Handler=null):void{
if(window.conch)
{
var folder:String=window.conch.getCachePath()+"/download/";
if (!window.fs_exists(folder)) {
window.fs_mkdir(folder);
}
var fileName:String = url.replace("http","").replace(/:/g,"").replace(new RegExp('/','g'),"_");
var cachepath:String = folder+fileName;
//判断文件是否存在
if (window.fs_exists(cachepath)) {
onComplete && onComplete.runWith(cachepath);
return;
}
var f:* = new window.conch_File(url);
var fr:* = new window.conch_FileReader();
fr.setIgnoreError(true);
fr.onload = function():void {
if(fr.result){
window.fs_writeFileSync(cachepath, fr.result);
onComplete && onComplete.runWith(cachepath);
}
else
{
onError && onError.runWith("no data");
}
};
// fr.onprogress = onprog;
fr.onerror = function(e):void{
onError && onError.runWith(e);
};
fr.readAsArrayBuffer(f);
}
else
{
onComplete && onComplete.runWith(url);
}
}
上面的代码是下载,在onComplete会返回一个保存的路径,在Android上经测试是在
/data/user/0/com.layabox.game//LayaCache//appCache/download/1.png 但在用Laya.loader.load在加载时,会显示加载出错,并输出了出错的地址:
http://stand.alone.version/data/user/0/com.layabox.game//LayaCache//appCache/download/1.png 不知道是不是Laya.loader.load是不是不能加载缓存中的资源?如果Laya.loader.load中的cache为true,能在Native下也有用,那就太好了。 毕竟有些项目并不需要打dcc,但是又需要缓存文件,比如我们这种资源有几十万个的,不知道有没有解决办法。