'Android - listFiles() doesn't list some files
I'd like to make an Android app to list files and their size, a bit like WinDirStat or TreeSize on Windows, or DiskUsage on Android.
I tried this to get a list of files :
for (file in File("/sdcard").listFiles()) {
println(file)
}
But it gives only the directories
+-----------------------------------------------+
| Expected output | Output |
+-----------------------------------------------+
| Alarms | DCIM |
| Android | Pictures |
| Audiobooks | ZEPETO |
| Aurora | Audiobooks |
| DCIM | Solid Color Wallpaper |
| Documents | Snapchat |
| Download | Aurora |
| EnergizedProtection | aria2 |
| Movies | Ringtones |
| Music | TWRP |
| Notifications | Documents |
| Pictures | Music |
| Podcasts | Telegram |
| Ringtones | Notifications |
| Snapchat | WhatsApp |
| Snapseed | Download |
| Solid Color Wallpaper | Movies |
| TWRP | Android |
| Telegram | EnergizedProtection |
| WhatsApp | Snapseed |
| ZEPETO | Podcasts |
| aria2 | Alarms |
| out.txt | |
| thisisafile.txt | |
+-----------------------------------------------+
I have the <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and the one for Android 10
EDIT :
I tested with file.walk, according to djacobsen13, and it too doesn't list out.txt
nor thisisafile.txt
. But it lists other .txt files (deeply in the cache or Android folders), so I conclude it's not about directories or not, but just not listing some files for strange reasons
EDIT 2 :
As blackapps said there's no description of the problem I add it here, I want to list all the files of the folder ("/sdcard" here or any other folder in it), but it returns only the folders and not the files
I put the app on GitHub : https://github.com/victorbnl/andirstat
EDIT 3 :
It lists all the files on Android 9
Solution 1:[1]
I set the minimum SDK to 28 so that I use the permissions system from Android Pie, which is easier. I'm going to use this, at least as a temporary solution, before the Android 11 system is more documented
Solution 2:[2]
Try using this logic to achieve what you're trying to do.
File("${Environment.getExternalStorageDirectory()}").walk().forEach {
println(it)
}
Solution 3:[3]
You can use recursion to go inside the directories and fetch all files from there.
Solution 4:[4]
Android 11 has added new permission settings: "Access to all files "
Special permissions: You need to apply dynamically in the code, and request in the form of skipping system activities
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && !Environment.isExternalStorageManager()) {
// Access to all files
val uri: Uri = Uri.parse("package:" + BuildConfig.APPLICATION_ID)
val intent = Intent(android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION, uri)
requireContext().startActivity(intent)
}
Works fine in my code.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Kureteiyu |
Solution 2 | djacobsen13 |
Solution 3 | Danish |
Solution 4 | Longge |