Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 502
Kureteiyu Avatar asked Nov 24 '25 11:11

Kureteiyu


2 Answers

Try using this logic to achieve what you're trying to do.

File("${Environment.getExternalStorageDirectory()}").walk().forEach {
      println(it)
}
like image 113
djacobsen13 Avatar answered Nov 26 '25 01:11

djacobsen13


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

like image 23
Kureteiyu Avatar answered Nov 26 '25 00:11

Kureteiyu