[英]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.