Android 앱을 만들고 디렉토리에 파일을 나열하고 싶습니다. 그때디렉토리에서 호출 될 때 File.listFiles가 null을 반환 할 수있는 이유는 무엇입니까?
int length = files.length;
를 호출하여
files
배열의 길이를 얻을 때
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
를 호출하여 만든 File
목적은, 내가
File[] files = path.listFiles(new CustomFileFilter());
path
를 호출하여이 작업을 수행 할 것입니다
files
이 null이기 때문에이 줄은 NullPointerException
이됩니다.
내가 확인 한 나는
System.out.println("Path exists: " + path.exists());
를 호출에 존재하는 파일을 나열하려고 내가 응용 프로그램을 실행할 때, 그것은 안드로이드 스튜디오 콘솔에서
Path exists: true
를 인쇄 path
경우, 그래서 디렉토리가 존재합니다.
또한
/storage/emulated/0/Download
는 그래서
path
디렉토리, 그리고 파일입니다 경로 이름을 인쇄했습니다.
path
은 (는) 디렉토리이므로 NullPointerException
을 얻는 이유에 대해 잘 모릅니다.
편집는 다음 CustomFileFilter
클래스는 다음과 같습니다
public class CustomFileFilter implements FileFilter {
// Determine if the file should be accepted
@Override
public boolean accept(File file) {
// If the file isn't a directory
if(file.isDirectory()) {
// Accept it
return true;
} else if(file.getName().endsWith("txt")) {
// Accept it
return true;
}
// Don't accept it
return false;
}
}
아마도 'CustomFileFilter' 클래스로 인해 Array에 파일이 없습니다. 'path.listFiles()'메소드로 같은 코드를 시도해보십시오. –
오, 질문에'CustomFileFilter' 클래스를 추가하는 것을 잊지 마십시오. –
'path.isDirectory()'와'path.canRead()'의 결과를 보여줍니다. 파일이라고 말하는 것만으로 파일이되지는 않습니다. – chrylis