How to download and save files (.json/.png/.jpg) from server into local storage in native platform(Android and IOS)

How to download and save files (.json/.png/.jpg) from server into local storage in native platform(Android and IOS)
0

#1

Hello,

I am developing a cross platform game template using CocosCreator + typescript . I need to allow user to choose theme from multiple theme and when user select any theme then only download the needed resources from server. I don’t want app to be restart after download resources game must run directly.

I have used below code to load resource from server.

var remoteUrl = "http://www.mywebsite.com/res/background/background.png";
cc.loader.load({url: remoteUrl, type: 'png'},(err , texture) => {
          if(err) {
                cc.error("Error : "+ (err.message || err));
          }
          else {
                // I want to store texture as .png in native plateform.
          }
     });

But i don’t know how i can store/save files into application resources in native platfrom Android and IOS.

So it will be great if someone can have any suggestions on it.

Thanks in advance.


#2

Maybe you could use cc.sys.localStorage, but you may have to convert the data before storing and after receiving, and I don’t know much about the persistency of it (I guess everything stored with it will be stored into the cache).

If you want to actually access the storage of the phone, I guess you would have to add this functionality in the native projects by yourself.


#3

https://cocos2d-x.org/reference/html5-js/V3.8/symbols/jsb.fileUtils.html

Try it out
You can save files to your device

server load -> local file save -> local file load
ex)
save-
let filePath = jsb.fileUtils.getWritablePath() + ‘render_to_sprite_image.png’;
let success = jsb.saveImageData(picData, 800, 400, filePath)

load-
let filePath = jsb.fileUtils.getWritablePath() + ‘render_to_sprite_image.png’;
cc.loader.load(filePath, (err, res) => {
this.label.string = “loadComp”;
this.sprite.spriteFrame = new cc.SpriteFrame(res);
})