user268397
user268397

Reputation: 1927

Why is listFiles() method returning null?

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

Answers (1)

CommonsWare
CommonsWare

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

Related Questions