Xobtah
Xobtah

Reputation: 545

Unable to open android's root folder

I want to make an explorer app. To give me a rough example I have the Simple File Manager app that shows my phone's file system :

enter image description here

enter image description here

I get the same result from my computer's file explorer when I plug the phone through USB :

enter image description here

So I have added a few permissions in my AndroidManifest.xml file :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And of course I made sure to ask the permissions to the user (although I'm not sure if I should ask for the Manifest.permission.WRITE_EXTERNAL_STORAGE permission as well, I want to be able to write too, but the authorisation popup doesn't mention read/write, it just asks for access to the storage) :

if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), STORAGE_PERMISSION_CODE)
}

But then I'm trying to get the content of the root folder, the one on the screenshots, I've tried to open a few this way :

Environment.getDataDirectory().listFiles() // Is null
Environment.getRootDirectory().listFiles() // Isn't the correct folder
Environment.getExternalStorageDirectory().listFiles() // Null
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).listFiles() // Null

I have tried almost every interesting paths found on this gist (https://gist.github.com/lopspower/76421751b21594c69eb2), but none of them give me what I want...

Upvotes: 0

Views: 946

Answers (1)

Try the following :

 String path = Environment.getExternalStorageDirectory().getAbsolutePath();
            File[] files = new File(path).listFiles();
            if (files != null && files.length > 0) {
                for (File file : files) {
                    Log.d("FILE", "--> "+file.getName());
                }
            }

Make sure the storage permission is granted, and add this line to your manifest : android:requestLegacyExternalStorage="true" within the App tag.

Logcat :

2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> Android
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> MIUI
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> dctp
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> did
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> Movies
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> DCIM
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> Pictures
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> Download
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> WhatsApp
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> ClassicGames
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> .wallpaperboard
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> com.facebook.katana
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> FileExplorer
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> logger
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> .turing.dat
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> MicTest
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> Telegram
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> IMG_20210206_210449.jpg
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> voip-data
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> Music
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> CamScanner
2021-02-27 23:55:35.470 3285-3285/maa.abc D/FILE: --> .BackgroundChange

Upvotes: 1

Related Questions