파일이 있고 IS 디렉토리가 있지만 listFiles ()가 null을 반환합니다.
에 대한 문서는File.listFiles()
이를 null
호출하는 파일이 디렉토리가 아닌 경우에만 반환 될 것을 제안합니다 .
다음이 있습니다.
String dir = "/storage/emulated/0";
File f = new File(dir);
Log.v("Files",f.exists()+"");
Log.v("Files",f.isDirectory()+"");
Log.v("Files",f.listFiles()+"");
로그는 다음과 같습니다.
true
true
null
어떤 이유로 listFiles(
)이 (가) 유효한 디렉토리로 인식 null
되어도 반환 File
됩니다. 저는 Android 파일 계층 동작에 익숙하지 않으므로 문제가 거기에 있다고 생각합니다.
참고로 Moto X에서 디버깅 중이며 전화가 컴퓨터에 연결되어 있는지 여부에 관계없이 결과가 동일하므로 연결했을 때 장착과 관련이 없다고 생각합니다.
이 문제가있는 경우 AndroidManifest.xml에 다음을 추가하십시오.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
해결 된 문제 : D
편집 : 이것이 작동하지 않으면 경로가 올바른지 확인하십시오.
Android 버전 23 이상의 경우 아래와 같이 onresume 메소드에서 프로그래밍 방식으로 런타임 권한 을 부여해야 합니다.
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));
}
이 문제가있는 경우 읽기 권한을 부여하려면 다음을 AndroidManifest.xml에 추가하십시오.
android:name="android.permission.READ_EXTERNAL_STORAGE"
또한 권한을 부여해야합니다.
설정> 앱> '앱 이름' > 권한> 저장 용량
File 'listFiles'메소드 문서에서 :이 추상 경로 이름이 디렉토리를 나타내지 않거나 I / O 오류가 발생하면 null을 반환합니다.
따라서 아마도 I / O 오류가 발생했습니다. 해당 디렉토리에 대한 권한을 확인하십시오. Java 프로그램을 실행하는 사용자에게 해당 디렉토리를 읽을 수있는 권한이 있습니까? 그렇지 않다면 답이 있습니다. 그렇다면 아마도 디렉토리가 비어있을 것입니다.
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 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.
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("\"","");
참고URL : https://stackoverflow.com/questions/20714058/file-exists-and-is-directory-but-listfiles-returns-null
'developer tip' 카테고리의 다른 글
정규 표현식으로 겹치는 일치를 찾는 방법은 무엇입니까? (0) | 2020.11.27 |
---|---|
요소를 기울이면서 텍스트를 정상적으로 유지하는 방법 (비뚤어지지 않음) (0) | 2020.11.27 |
React Native-동적 이름을 사용하는 이미지 요구 모듈 (0) | 2020.11.27 |
Numpy의 0d 배열이 스칼라로 간주되지 않는 이유는 무엇입니까? (0) | 2020.11.27 |
자식 푸시 후 로컬 실행 후크? (0) | 2020.11.27 |