简体   繁体   中英

How to access directory “/storage/emulated”

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.

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"/>

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

Your app does not have read access to that directory.

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.");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM