Jsb.fileUtils.writeToFile crash Android

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 encounted the same problem, when i use jsb.fileUtils.writeToFile the ios simulater will crash too.
Do you find any method to solve this problem?

the cocos2d-js version what i use is 3.1.

I tryed many methods, but i failed. Also tryed jsb.reflect and write save/read code in java, but i had problems with it too.

I can create folder on “/sdcard”, but when i try save file then app crash.

Simple code:

    if (cc.sys.isNative) {
        var path = jsb.fileUtils.getWritablePath();
        cc.log('PATH: ' + path);
        var file = jsb.fileUtils.writeToFile(path+'abc', path+'abc');
        cc.log(file);
    }

after launch its restarts several times smartphone and always give me log:

D/cocos2d-x debug info( 2090): JS: PATH: “/data/data/com.example.game/files/”
I/DEBUG ( 104): #00 pc 82ff4bf0 /data/data/com.example.game/lib/libcocos2djs.so
I/DEBUG ( 104): #01 lr 82f8cddc /data/data/com.example.game/lib/libcocos2djs.so
I/DEBUG ( 104): 48162a9c 83282be0 /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162ab4 83282ba0 /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162ad4 83282b28 /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162af4 833b3c9c /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162b04 82a428dc /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162b24 82f8cddc /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162b38 82a428dc /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162b4c 829a1c54 /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162b54 831bdf60 /data/data/com.helidev.pirategame/lib/libcocos2djs.so
I/DEBUG ( 104): 48162b7c 831bddf8 /data/data/com.helidev.pirategame/lib/libcocos2djs.so

Hi

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 :smile: . If anyone has an idea how to save strings or arrays otherwise to the filesystem would be nice to share here :smile:

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 :smile: . For me it was perfect. Perhaps it helps anybody else who finds this thread like me…

1 Like

Reyka, i owe you a big beer!
I red source code, but i tryed to pass tilemap file intead of just array.
When i pass array everything works perfect. :smile:

For guys with the same problem:

Sample code to write and read saved data:

    if (cc.sys.isNative) {
        var path = jsb.fileUtils.getWritablePath();
        var file = jsb.fileUtils.writeToFile({test:'test2'}, path+'abc');
        var string1 = jsb.fileUtils.getValueMapFromFile(path+'abc');
        cc.log('string1: ' + JSON.stringify(string1));
    } 

Remember - when you reinstall app, existing files in that app folder are removed.

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

log ==>false