18

So I am trying to get a list of files in a directory with a file handle.list() method but it returns an empty list even though there are files in the directory. What seems strange to me is that it does work on the device but it does not work on the desktop. I think I know what the problem is but I dont know how to solve it though. In the method description it says "On the desktop, an FileType.Internal handle to a directory on the classpath will return a zero length array.", but there is no other method than this so what do you guys think I can do?

Daahrien
  • 10,190
  • 6
  • 39
  • 71
Leonso Medina Lopez
  • 777
  • 1
  • 10
  • 21

1 Answers1

23

The "internal files" are found via the classpath when run on the desktop, so there is no simple way to "list" a directory in the classpath. If you're just using the desktop for development, and don't mind some hacks you can search "./bin/" for the missing files.

Like this:

FileHandle dirHandle;
if (Gdx.app.getType() == ApplicationType.Android) {
   dirHandle = Gdx.files.internal("some/directory");
} else {
  // ApplicationType.Desktop ..
  dirHandle = Gdx.files.internal("./bin/some/directory");
}
for (FileHandle entry: dirHandle.list()) {
   // yadda ...
}

For a bit more detail, see: http://bitiotic.com/blog/2012/05/15/libgdx-internal-files-hacks/

Update: this is not correct any more. That "./bin/" path prefix don't have to be added - works well without adding it and not working when it's added. So this solution is obsolete.

MilanG
  • 6,994
  • 2
  • 35
  • 64
P.T.
  • 24,557
  • 7
  • 64
  • 95
  • @iLoveUnicorns - the Android path will work fine on iOS too (I just tested it). – sunil Sep 21 '15 at 17:12
  • As a side-note, if actually using the file entry, do it like this: `Gdx.files.internal(entry.file().getAbsolutePath().substring(1))` as without eliminating the first char (which is a '/'), it will crash on android. – Cryptoclysm Jan 08 '16 at 20:55
  • 1
    Building the application for desktop doesn't allow this. I do not know why but it seems like .list() is not returning any FileHandle – Martacus Sep 19 '18 at 20:42