Search code examples
androidlibgdx

libGDX android assets file not found


I'm testing my app (with android, core and desktop modules) on desktop, everything works fine.

Now that I try to run on Android, it gives me FileNotFound. I linked android asset to desktopLauncher before running on desktop, but Am I meant to do something similar for android?

            File file = Gdx.files.internal("liver_initial_joints.json").file();
            FileInputStream fis = new FileInputStream(file);
            byte[] data = new byte[(int) file.length()];
            fis.read(data);
            fis.close();

            str = new String(data, "UTF-8");

I have my file at .../android/assets/liver_initial_joints.json


Solution

  • Android does not provide direct java.io.File access to the internal app directory. If you look at the javadocs for libgdx's FileHandle.file(), you'll see that it says it's not usable for anything but Absolute and External types of files.

    If you just need to read a string or byte array, you can use fileHandle.readString() or fileHandle.readBytes(). If you need an input stream, use fileHandle.read();.

    For example:

            byte[] data = Gdx.files.internal("liver_initial_joints.json").read();
            str = new String(data, "UTF-8");