Alan Wernick
Alan Wernick

Reputation: 11

LibGDX not using Android asset folder

I am having a problem with a libGDX project reading the assets folder when compiling the Android or IOS build. While trying to access a file in the assets folder under the android module, I always seem to get a FileNotFoundException in IOS or Android, but not when running it on the Desktop. My desktop config points the android asset directory like it should.

When executing the following code :

 public void readGameMap() throws IOException
    {
        readGameMap("gameMap.txt");
    }

    public void readGameMap(String path) throws IOException
    {

        Scanner s = new Scanner(Gdx.files.internal(path).file());


        int i = 0;


        while ((s.hasNextLine()))
        {

            String str = s.nextLine();

            if (str.length() < maze.length)
                throw new IOException("Incorrect game_map.txt structure");

            for (int j = 0; j < str.length(); j++)
            {

                Object temp;

                switch (str.charAt(j))
                {
                    case '1':
                        temp = new Wall();
                        break;

                    case '3':
                        temp = new Dot();
                        break;

                    default:
                        temp = new Object();
                        break;
                }

                maze[j][i] = temp;

            }

            i++;
        }

        s.close();

    }

The file not found exception is thrown. My folder structure is as follows:

-Android
  -assets
    -gameMap.txt
-Desktop
-IOS
-HTML
-core

Upvotes: 1

Views: 1979

Answers (2)

kalan nawarathne
kalan nawarathne

Reputation: 2014

In case if you are using Android Studio, select edit config... In case if you are using Android Studio, select edit config...

Change 'Desktop Application' -> 'Working Directory' to your android asset forlder Change 'Desktop Application' -> 'Working Directory' to your android asset forlder

Upvotes: 5

Ocfmem
Ocfmem

Reputation: 356

You should use Gdx.file.internal();

https://github.com/libgdx/libgdx/wiki/File-handling#reading-from-a-file

If you used the [setup UI | Project Setup, Running & Debugging], this file will be contained in your Android project's assets folder, $ANDROID_PROJECT/assets/data to be exact. Your desktop and html projects link to this folder in Eclipse, and will pick it up automatically when executed from within Eclipse.

Upvotes: 0

Related Questions