3

I'm on Marshmallow(api 23).

And I've declared the relative permissions and request these permissions in runtime. I've got the external storage directory——"/storage/emulated/0" through Environment.getExternalStorageDirectory(). Using this file instance(here I call it extF), extF.listFiles() is not null. But extF.getParentFile().listFiles() returns null.

In adb shell, I have checked the "/storage/emulated" directory and there did have two child directories:

0

obb

So, why can't I get the children files of "/storage/emulated" on Marshmallow(api23) through File.listFiles()? Thank you for your explanation.

Lym Zoy
  • 951
  • 10
  • 16

3 Answers3

1

you can get your list of files in marshmallow like this

    //this function will check for runtime permission so Add this block in your OnCreate() Method

    if(checkandRequestPermission()) {

        File sdCardRoot = Environment.getExternalStorageDirectory();
                Log.w("SDCardRoot", "" + sdCardRoot);
                File yourDir = new File(sdCardRoot, "/yourFolderName");

                // This will check if directory is exist or not
                if (!yourDir.exists()) {
                    yourDir.mkdirs();
                }

                if(yourDir.isDirectory()){

                    File[] content = yourDir.listFiles();
                    for(int i = 0; i<content.length;i++){

                        // Your List of Files
                        Log.w("List", "" + String.valueOf(content[i]));

                    }
    }

Now If you are using api level 23 then you need to add runtime permission like this

// Declare this function to check for runtime permission 

private boolean checkandRequestPermission(){

        int storageread = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);

        List<String> listpermissionNeeded = new ArrayList<>();

        if (storageread != PackageManager.PERMISSION_GRANTED) {
            listpermissionNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
        }


        if (!listpermissionNeeded.isEmpty()) {
            ActivityCompat.requestPermissions(this, listpermissionNeeded.toArray(new String[listpermissionNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
            return false;
        }
        return true;
    }

Add This permission in your Manifiest

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Piyush k
  • 297
  • 3
  • 14
  • In my android studio this code work perfectly. It access all list files from specified folder. If you want to access child folder then you need to specified perfect path of that directory.. – Piyush k Jul 05 '16 at 13:19
  • I want to get the `parent file` of your `sdCardRoot`, and then get its child files, which means the sibling files of your `sdCardRoot`. – Lym Zoy Jul 05 '16 at 13:23
  • If you want to access the parent files then in this code you need to add your `filename.extention` at the place of `/yourfoldername` and the get rid of `If else` block that checks for directory. Now it will check for parent files in sdcard. you can also find that in this link http://stackoverflow.com/questions/2902689/how-can-i-read-a-text-file-from-the-sd-card-in-android – Piyush k Jul 05 '16 at 13:30
  • You, simply, can't access to `/storage/emulated` unless your device is rooted. External storage is `/storage/emulated/0` not `/storage/emulated` – Farid Sep 07 '19 at 10:02
0

So, why can't I get these files through File.listFiles()?

Your app does not have read access to that directory.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • @lmz: On a rooted device, presumably there is an approach using `su`/`sudo`. Or, you can build your own custom ROM that makes this directory world-readable, then install that custom ROM on your device. Otherwise, you have no means of getting read access to it. – CommonsWare Jul 05 '16 at 13:15
0

May be problem with the Permissions in Manifest: android:name="android.permission.READ_EXTERNAL_STORAGE" *

If this does its not works just make sure if path is correct

Below code is working for me

File f = new File(Environment.getExternalStorageDirectory()+ "");
        if (f.exists()) {
        File[] dff= f.listFiles();
        }else{
            Logs.debug("siz","Files not existed.");
        }
Android Surya
  • 544
  • 3
  • 17
  • `Environment.getExternalStorageDirectory()` returns `/storage/emulated/0` not `/storage/emulated`. Android won't grant a permission to access to `/storage/emulated` unless it's rooted. – Farid Sep 07 '19 at 10:00