问题描述
所以我尝试使用文件 handle.list() 方法获取目录中的文件列表,但即使目录中有文件,它也会返回一个空列表.对我来说奇怪的是它可以在设备上工作,但不能在桌面上工作.我想我知道问题是什么,但我不知道如何解决它.在方法描述中它说在桌面上,类路径上目录的 FileType.Internal 句柄将返回一个零长度数组."但是除了这个之外没有其他方法,所以你们认为我能做什么?
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?
推荐答案
内部文件"是在桌面运行时通过类路径找到的,所以没有简单的方法可以在类路径中列出"一个目录.如果您只是使用桌面进行开发,并且不介意一些 hack,您可以在./bin/"中搜索丢失的文件.
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.
像这样:
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 ...
}
有关更多详细信息,请参阅:http://bitiotic.com/blog/2012/05/15/libgdx-internal-files-hacks/
For a bit more detail, see: http://bitiotic.com/blog/2012/05/15/libgdx-internal-files-hacks/
更新:这不再正确.不必添加./bin/"路径前缀 - 无需添加即可正常工作,添加后无法正常工作.所以这个解决方案已经过时了.
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.
这篇关于Libgdx 如何获取目录中的文件列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!