Search code examples
javaandroidandroid-studiolibgdxfile-not-found

Android studio "File not found" when using ADB, Desktop version works


Whenever I try to run my libdgx app on my phone via ADB, android studio is unable to find a file in the "android/assets" folder. However, when I run the desktop version it works fine.

I'm using this to read from a file:

File file = new File("BlocksProgression.txt");
reader = new BufferedReader(new FileReader(file));

As explained, this works fine when I run the desktop launcher, but the android launcher returns this error:

W/System.err: java.io.FileNotFoundException: BlocksProgression.txt (No such file or directory)

I've been searching for over an hour now, but I can't seem to find how to setup the assets folder correctly.

Any help would be appreciated.


Solution

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

    Turns out Libgdx wants you to use a FileHandle object to read the file. Using this my code becomes:

    FileHandle file = Gdx.files.internal("BlocksProgression.txt");
    String data = file.readString();
    

    And this simply returns the text as string. Hope this can help some people out.