Reputation: 1927
I'm wondering why the listFiles() method is returning null? I am using the this string for a file path: /storage/UsbDriveA.
Here is the code I'm currently using:
List<String> filesInFlashDrive = addListOfFiles("/storage/UsbDriveA/");
public ArrayList<String> addListOfFiles(String directoryPath) {
File f = new File(directoryPath);
f.mkdirs();
Log.i("FileBrowserActivity", "File Value:" + f);
Log.i("FileBrowserActivity", "List of files:"+f.listFiles());
File[] file = f.listFiles();
/*File[] file = f.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.toString().endsWith(".pdf") ? true : false;
}
});*/
ArrayList<String> arrayFiles = new ArrayList<String>();
if (file.length == 0)
return null;
else {
for (int i=0; i<file.length; i++)
arrayFiles.add(file[i].getName());
}
return arrayFiles;
}
Why is the listFiles() method returning null?
Upvotes: 0
Views: 273
Reputation: 1006674
First, your device or emulator may not have such a path. Very few devices do.
Second, because that appears to be removable storage, you do not have arbitrary filesystem access to it on Android 4.4+ devices.
Upvotes: 3