文件存在且是目录,但listFiles()返回null [英] File exists and IS directory, but listFiles() returns null
问题描述
的文档 File.listFiles()
表明,空
将只在该文件调用它的情况下返回是不是一个目录。
The documentation for File.listFiles()
suggests that null
will ONLY be returned in the case that the file calling it is not a directory.
我有以下几点:
String dir = "/storage/emulated/0";
File f = new File(dir);
Log.v("Files",f.exists()+"");
Log.v("Files",f.isDirectory()+"");
Log.v("Files",f.listFiles()+"");
日志内容如下:
The log reads:
true
true
null
由于某种原因, listFiles(
)将返回空
尽管文件
被识别为有效的目录。我不是超级熟悉Android的文件层次结构的行为,所以我猜问题出在那里。
For some reason, listFiles(
) is returning null
even though the File
is recognized as a valid directory. I'm not super familiar with Android file hierarchy behavior, so I would guess the problem lies in there.
有关的参考,我调试我的极限摩托,和结果都是一样的手机是否插在我的电脑或不 - 。所以我不认为它必须与时插入安装
For reference, I'm debugging on my Moto X, and results are the same whether the phone is plugged in to my computer or not - so I don't think it has to do with mounting when plugged in.
推荐答案
对于这个问题,这增加的AndroidManifest.xml:
For those with this problem, add this to AndroidManifest.xml:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
问题解决了:D
Problem solved :D
编辑:如果这不起作用只要确保,如果路径是正确的。
If this does not work just make sure if path is correct
这篇关于文件存在且是目录,但listFiles()返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!