简体   繁体   English

Android File.listFiles() 仅返回子目录

[英]Android File.listFiles() returns only subdirectories

I am trying to get all files and subdirectories in directory and show their names.我正在尝试获取目录中的所有文件和子目录并显示它们的名称。 This is the code I use:这是我使用的代码:

File internalStorageDir = new File(Environment.getExternalStorageDirectory().getPath());
File[] files = internalStorageDir.listFiles();
ArrayList<String> fileNames = new ArrayList<>();
for(int i=0; i< files.length; i++){fileNames.add(files[i].getName());}

The specified directory(Environment.getExternalStorageDirectory()) contains files and subdirectories, but listFiles() only returns subdirectories.指定目录(Environment.getExternalStorageDirectory()) 包含文件和子目录,但 listFiles() 只返回子目录。 What's wrong with this code?这段代码有什么问题? Permission to read external storage has already been granted.已授予读取外部存储的权限。

My fault, I missed something.我的错,我错过了一些东西。 In Android 11, you can only access media files.在 Android 11 中,您只能访问媒体文件。 In my case, the files in external storage were not media files, so they were not displayed.就我而言,外部存储中的文件不是媒体文件,因此没有显示。 But after changing the target api to 29 (Android 10) everything works fine.但是在将目标 api 更改为 29 (Android 10) 后一切正常。 Thanks to everyone who tried to help!感谢所有试图提供帮助的人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM