文件存在且是目录,但 listFiles() 返回 null [英] File exists and IS directory, but listFiles() returns null

查看:104
本文介绍了文件存在且是目录,但 listFiles() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

File.listFiles() 的文档 表示 null 仅在调用它的文件不是目录的情况下才会返回.

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()+"");

日志内容:

true
true
null

出于某种原因,即使 File 被识别为有效目录,listFiles() 也会返回 null.我不太熟悉 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.

作为参考,我正在 Moto X 上调试,无论手机是否插入我的计算机,结果都是一样的 - 所以我认为这与插入时的安装无关.

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

如果这不起作用,请确保路径是否正确

If this does not work just make sure if path is correct

这篇关于文件存在且是目录,但 listFiles() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆