skip to Main Content

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


  1. Chosen as BEST ANSWER

    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


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

    File("${Environment.getExternalStorageDirectory()}").walk().forEach {
          println(it)
    }
    
    Login or Signup to reply.
  3. You can use recursion to go inside the directories and fetch all files from there.

    Login or Signup to reply.
  4. Android 11 has added new permission settings: "Access to all files "

    Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search