Timberwolf
Timberwolf

Reputation: 436

Android: isDirectory() always returning true

I tried here, but it did not help.

My filename filter to get directories and .txt files only:

FilenameFilter filter = new FilenameFilter()
{
    public boolean accept(File dir, String name)
    {
        if (dir.isDirectory())
        {
            return true;
        }
        else
        {
            return dir.getName().endsWith(".txt");
        }
    }
};

I also tried !dir.isFile()

Applying the filter to the list of files and directories:

            CurDir = homeDir;
            dir = new File(homeDir);
            values = dir.list(filter);
            if (values == null)
            {
                Toast.makeText(this, "No Files/Folders", Toast.LENGTH_LONG).show();
            }
            else
            {
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
                setListAdapter(adapter);
            }

Value of homeDir:

homeDir = Environment.getExternalStorageDirectory().toString();

It still shows .png and all other files.

Upvotes: 0

Views: 649

Answers (1)

Alex Coleman
Alex Coleman

Reputation: 7326

The File dir passed is the directory containing the actual file, so it will always be a directory. The full file you have is %dir%/%name%. I believe name will be "/" or perhaps null if the overall file is a directory though.

You could also create the full file by doing new File(dir, name);

Upvotes: 2

Related Questions