Libgdx How to get a list of files in a directory?

混江龙づ霸主 提交于 2020-01-21 02:43:06

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/12681678/libgdx-how-to-get-a-list-of-files-in-a-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!