Hello guys, maybe you will have idea what i do wrong.
I use Cocos2d JS v3.0, tried on phones with Android 2.3.6 and 4.4.4
Problem is when i try to save data on Android device using ** jsb.fileUtils.writeToFile ** (when i try another function: ** jsb.fileUtils.writeStringToFile **, there is an error âTypeError: jsb.fileUtils.writeStringToFile is not a functionâ).
Simple code:
_save: function () {
if (cc.sys.isNative) {
var path = jsb.fileUtils.getWritablePath();
cc.log('PATH: ' + JSON.stringify(path)); //it works
cc.log('FILESIZE: ' + JSON.stringify(jsb.fileUtils.getFileSize(path))); //it works too
jsb.fileUtils.writeToFile(path + '/test.txt', path + '/test.txt'); //aplication crash
}
}
âadb logcatâ shows:
âŚ
D/cocos2d-x debug info( 6369): create rendererRecreatedListener for GLProgramState
D/CCFileUtilsAndroid.cpp( 6369): relative path = res/img/exit.png
D/cocos2d-x debug info( 6369): JS: PATH: â/data/data/com.example.egame/files/â
D/cocos2d-x debug info( 6369): JS: FILESIZE: 4096
D/PowerManagerService( 182): releaseWakeLock flags=0x1 tag=Icing
I/GoogleHttpClient( 3378): Falling back to old SSLCertificateSocketFactory
W/ConfigFetchTask( 3378): exception on config fetch: java.net.UnknownHostException: android.clients.google.com
W/ConfigFetchTask( 3378): java.net.UnknownHostException: android.clients.google.com
W/ConfigFetchTask( 3378): at java.net.InetAddress.lookupHostByName(InetAddress.java:499)
W/ConfigFetchTask( 3378): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:296)
W/ConfigFetchTask( 3378): at java.net.InetAddress.getAllByName(InetAddress.java:258)
âŚ
Do you have idea what could be wrong with my usage of ** jsb.fileUtils.writeToFile ** function? Please paste code how you use this function or any game source where save to file is used. This would be very helpful.
I am new in developing with cocos2d but i partially succeeded in saving a file with writeToFile().
When i tried to save a string with that function, my app crashed too.
When i tried to save an array it succeeded - but it always saves an empty plist file no matter what the content of the array is.
i assume this function is not to save âany dataâ or string or whatever to the filesystem. In the documentation here we can see that the arg0 is of type âmap_objectâ. I therefore assume this function is only for specifit map objects and it converts that file to a plist file. (So that conversion crashes with a string but not with an array maybe?)
unfortunately i didnât find another function to save data to the filesystem (but i found this thread . If anyone has an idea how to save strings or arrays otherwise to the filesystem would be nice to share here
oh well i found something here. You can save it to the localstorage. I didnât test it on android but on ios it saves that data into the jsb.sqlite database wich is found in the Documenst folder of the application. so you could save any data by
cc.sys.localStorage.setItem("key", value);
and retreive it by
var savedValue = cc.sys.localStorage.getItem("key");
I tried it with a json file thus JSON.strigify() and JSON.parse() the value. worked like a charm. But I donât know if this suits your needs or itâs already too late . For me it was perfect. Perhaps it helps anybody else who finds this thread like meâŚ
I want to move js file or write data from one js file to another
can someone slove it
> if (cc.sys.isNative) {
> cc.log("==>"+ jsb.fileUtils.writeStringToFile(âfineâ, âsrc/2.txtâ));
> }