File exists and IS directory, but listFiles() returns null File exists and IS directory, but listFiles() returns null android android

File exists and IS directory, but listFiles() returns null


For those with this problem, add this to AndroidManifest.xml:

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

Problem solved :D

EDIT: If this does not work just make sure if path is correct


In addition to other answers and comments, I'd suggest you check whether you need

<application android:requestLegacyExternalStorage="true"

in your manifest. It seems that some recent apis need it.

Anyway, zapl's comment is rather short but quite insightful.You can "ls -ld" the directory on the device (via "adb shell" or some other shells).If you have "r" permission on the directory, you can call listFiles(). Otherwise, it returns null. Note that you can access files under the unreadable directory if you know the file names and have "x" permission on the directory.You can know who you are by "whoami" and "groups" commands.


For android version 23 or higher, you need to give run time permission programmatically in onresume method as below,

public final String[] EXTERNAL_PERMS = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE};public final int EXTERNAL_REQUEST = 138;requestForPermission();public boolean requestForPermission() {    boolean isPermissionOn = true;    final int version = Build.VERSION.SDK_INT;    if (version >= 23) {        if (!canAccessExternalSd()) {            isPermissionOn = false;            requestPermissions(EXTERNAL_PERMS, EXTERNAL_REQUEST);        }    }    return isPermissionOn;}public boolean canAccessExternalSd() {    return (hasPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE));}private boolean hasPermission(String perm) {    return (PackageManager.PERMISSION_GRANTED == ContextCompat.checkSelfPermission(this, perm));}