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

只谈情不闲聊 提交于 2019-11-28 18:31:30

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

varotariya vajsi

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));

}
Ravi Joshi

For those with this problem you to give read permission, add this to AndroidManifest.xml:

android:name="android.permission.READ_EXTERNAL_STORAGE" 

and you should also grant permission from

Settings > Apps > "Your app name" > Permissions > storage

From the File 'listFiles' method documentation: Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.

So perhaps an I/O error occurred. Check the permissions on that directory, does the user running the java program have the permission to read that directory? If not, there is your answer. If they do, then perhaps the directory is empty.

BTW you should not use +"" to convert something to a string, it's a bad practice since it's slower than String.valueOf. I would use apache's StringUtils to join that array into a string, but that requires adding that jar to your classpath.

Just add the permission in manifest uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" and your problem will be resolved.

My problem was that for some reason the android:sharedUserId="android.uid.system" attribute was not allowing the app to read /sdcard/ on a specific device.

As I needed to do only a quick test, I removed the sharedUserId attribute, tested and added it back.

In the manifest file at the <uses-permission .... > section, add this line android:name="android.permission.READ_EXTERNAL_STORAGE". I think this may solve your problem.

I had a same problem in java but not in android where i am reading config file with path in quotes. I solved my issue with just removing quotes

So you can do it by String replace function or give it without quotes

String someString= "C:\path"; someString=someString.replace("\"","");

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!