Is there a way to save a file into internal storage (private mode) on Android?

Hi!

I’m looking for a way to save a file into internal storage on Android. I need this to store app’s settings. If I’ll store it on external memory - I think every user can remove this settings-file or reset some options there :slight_smile:

Where is you store your game settings?

For example, reading and writing into internal Android memory:

    String FILENAME = "hello_file";
    String string = "hello world!";

    try {

        FileOutputStream fos = openFileOutput( FILENAME, Context.MODE_PRIVATE );
        fos.write( string.getBytes() );
        fos.close();

        FileInputStream in = openFileInput( FILENAME );
        InputStreamReader inputStreamReader = new InputStreamReader(in);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            Log.e( "start", line );
        }

    } catch ( IOException e ) {

        Log.e( "error!", "error!" );

    }

So what about Cocos2d-x? :slight_smile:

To save program settings you can use CCUserDefault class. For example saving bool:
CCUserDefault::sharedUserDefault()->setBoolForKey(“FIRST_LAUNCH”, false);
CCUserDefault::sharedUserDefault()->flush();
and reading this bool:
bool firstLaunch = CCUserDefault::sharedUserDefault()->getBoolForKey(“FIRST_LAUNCH”, true);

Leszek X wrote:

To save program settings you can use CCUserDefault class. For example saving bool:
CCUserDefault::sharedUserDefault()->setBoolForKey(“FIRST_LAUNCH”, false);
CCUserDefault::sharedUserDefault()->flush();
and reading this bool:
bool firstLaunch = CCUserDefault::sharedUserDefault()->getBoolForKey(“FIRST_LAUNCH”, true);

Wow. Awesome. So easy! Thank you!

If you want to use your own XML, try using CCFileUtils::getWriteablePath()

Lance Gray wrote:

If you want to use your own XML, try using CCFileUtils::getWriteablePath()

I think the files from the CCFileUtils::getWriteablePath() can be removed at any time. Am I right?

I’m not sure about Android but on the iOS the user can’t. Unless the user does some computer voodoo on it, I guess it’s not that big of a problem.

Lance Gray wrote:

I’m not sure about Android but on the iOS the user can’t. Unless the user does some computer voodoo on it, I guess it’s not that big of a problem.

Yeah, that’s not a big problem with iOS but on Android user has more control on deleting some important data for the app :slight_smile: