Android file chooser with specific file extensions

It’s an unknown to you what user-installed file browser apps your intent may trigger. I think this is a case where’s it’s better to hand roll something. My approach was to

a) Find all files with a particular extension on external media with something like (I was looking for the .saf extension, so you’d alter for .pdf accordingly):

    public ArrayList<String> findSAFs(File dir, ArrayList<String> matchingSAFFileNames) {
    String safPattern = ".saf";

    File listFile[] = dir.listFiles();

    if (listFile != null) {
        for (int i = 0; i < listFile.length; i++) {

            if (listFile[i].isDirectory()) {
                findSAFs(listFile[i], matchingSAFFileNames);
            } else {
              if (listFile[i].getName().endsWith(safPattern)){
                  matchingSAFFileNames.add(dir.toString() + File.separator + listFile[i].getName());
                  //System.out.println("Found one! " + dir.toString() + listFile[i].getName());
              }
            }
        }
    }    
    //System.out.println("Outgoing size: " + matchingSAFFileNames.size());  
    return matchingSAFFileNames;
}

b) Get that result into a ListView and let the user touch the file s/he wants. You can make the list as fancy as you want — show thumbnails, plus filename, etc.

It sounds like it would take a long time, but it didn’t and you then know the behavior for every device.

Leave a Comment