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
4
Answers
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
Try using this logic to achieve what you’re trying to do.
You can use recursion to go inside the directories and fetch all files from there.
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
Works fine in my code.