简体   繁体   English

如何访问目录“/ storage / emulated”

[英]How to access directory “/storage/emulated”

I'm on Marshmallow(api 23) . 我在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() . 我通过Environment.getExternalStorageDirectory()获得了external storage directory - “/ storage / emulated / 0”。 Using this file instance(here I call it extF ), extF.listFiles() is not null. 使用这个文件实例(这里我称之为extF ), extF.listFiles()不为null。 But extF.getParentFile().listFiles() returns null. 但是extF.getParentFile().listFiles()返回null。

In adb shell, I have checked the "/storage/emulated" directory and there did have two child directories: 在adb shell中,我检查了"/storage/emulated"目录,并且确实有两个子目录:

0

obb

So, why can't I get the children files of "/storage/emulated" on Marshmallow(api23) through File.listFiles() ? 那么,为什么我Marshmallow(api23) File.listFiles()Marshmallow(api23)上获取"/storage/emulated"的子文件? Thank you for your explanation. 谢谢你的解释。

you can get your list of files in marshmallow like this 你可以像这样在marshmallow中获取你的文件列表

    //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 现在如果你使用的是api level 23,那么你需要像这样添加运行时权限

// 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 在Manifiest中添加此权限

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

So, why can't I get these files through File.listFiles()? 那么,为什么我不能通过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" * 可能是Manifest中的Permissions问题: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.");
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何访问 /storage/emulated/0/ - How to access /storage/emulated/0/ /storage/emulated/0/Notes/(没有那个文件或目录)? - /storage/emulated/0/Notes/ (No such file or directory)? 如何进入存储/模拟/0 - How to get to storage/emulated/0 如何从/ storage / emulated / 0访问文件列表 - How to get access to list of files from /storage/emulated/0 如何在android中创建外部存储目录,将外部存储路径显示为“storage / emulated / 0”? - How to create directory in external storage in android which show external storage path as “storage/emulated/0”? 如何使用存储访问框架访问“/storage/emulated/0/Android/media/”? - How to get access to "/storage/emulated/0/Android/media/" using Storage Access Framework? 存储/模拟/ 0 /打开失败:ENOENT(没有这样的文件或目录)? - storage/emulated/0/ open faild:ENOENT (no such file or directory)? 无法在模拟器上的 /storage/emulated/0 上创建目录 - Can't create a directory on /storage/emulated/0 on emulator 如何在 android 目录 storage/emulated/0/Android/data 上保存和写入数据 python - how to save and write data on android directory storage/emulated/0/Android/data with python 如何访问此内部存储目录? - How to access this internal storage directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM