Search code examples
javaandroidlibgdx

LibGDX Android create internal files on installation


I need several files to be in the internal directory of my Android app on installation.
I could theoretically just copy them from the assets folder but that is not really convenient.
The files would be to store settings and progression.

How could I create these files?


Solution

  • As mentioned in the comments, during installation, the device does nothing but simply copy the app itself. If you want to do anything else, that's the job of your application.

    Use local storage for this purpose (will be deleted on uninstall, on the desktop target, this is the same as internal.)

    You could either:

    1. check whether the files exist, and copy them if they do not

    This is the more reliable choice, as in the (rare?) case that the files are deleted, they will be replaced.

    @Override
    public void create() {
        ...
        for (String file : filesToCopy) {
            final FileHandle handle = Gdx.files.local(file);
            if (!handle.exists()) {
                // copy to handle
            }
        }
    }
    
    1. store whether or not the app has already been launched at least once, and if the key does not exist, copy the files

    You can use Preferences for this:

    @Override    
    public void create() {
        ...
        final Preferences prefs = Gdx.app.getPreferences("prefs");
        if (prefs.getBoolean("newInstall", false)) {
            for (String file : filesToCopy) {
                final FileHandle handle = Gdx.files.local(file);
                // copy to handle
            }
        }
        prefs.putBoolean("newInstall", true);
        prefs.flush();
    }