0

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.

Community
  • 1
  • 1
Timberwolf
  • 426
  • 4
  • 13

1 Answers1

2

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);

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31
  • No because when it loads it shows the root of the sd like I intended it to. – Timberwolf Oct 03 '12 at 01:31
  • Do some logging; log the dir and the name and check – Alex Coleman Oct 03 '12 at 02:08
  • I looked at the console tab and logcat and everywhere else in eclipse system.out.println doesnt work with android apps – Timberwolf Oct 03 '12 at 03:44
  • FilenameFilter filter = new FilenameFilter() { public boolean accept(File dir, String name) { File tmp = new File(dir.getAbsolutePath() + "/" + name); if (tmp.isDirectory()) { Log.i("Folder: ", tmp.getAbsolutePath()); return true; } else { Log.i("File: ", tmp.getAbsolutePath()); return name.endsWith(".txt"); } } }; – Timberwolf Oct 03 '12 at 16:40