Skip to content
Advertisement

listFiles() returns null on /storage/emulated, while /storage/emulated/0 exists

I am testing on android 27 emulator and trying to traverse some directories.

When I try to traverse /storage/emulated directory, the method listFiles() returns null while there /storage/emulated/0 exists.

Here is a testing code:

File emuDir = new File("/storage/emulated");
File[] emuDirFiles = emuDir.listFiles(); // returns null.

File exStorageDir = new File("/storage/emulated/0")
exStorageDir.exists(); // returns true

P.S. I have added the proper storage permission in manifest and granted the runtime permission.

Advertisement

Answer

Short answer: Android won’t grant your app a permission to access /storage/emulated.

Long answer and the reason your solution is not working:

Which permissions you’ve granted? I presume they’re EXTERNAL_WRITE/EXTERNAL_READ and those will give you access to /storage/emulated/0 which is the same directory returned by getExternalStorageDirectory(). Android doesn’t give read/write permission to /storage/emulated. As a side note, if File.canRead() returns false you can’t listFiles() of that directory. In your case, even if you have added permissions emuDir.canRead() will return false because those permissions grants access to /storage/emulated/0 not to /storage/emulated

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement