9

I need to show only 'pdf' files in my application when I run default File chooser I'm not able to filter file extensions.

    final Intent getContentIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getContentIntent.setType("application/pdf");
    getContentIntent.addCategory(Intent.CATEGORY_OPENABLE);

    Intent intent = Intent.createChooser(getContentIntent, "Select a file");
    startActivityForResult(intent, REQUEST_PDF_GET);

File chooser shows any kinds of files. I would like to show only pdf files. How can i filter files showed by File chooser.

Nande kore
  • 784
  • 1
  • 8
  • 16

3 Answers3

8

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.

JASON G PETERSON
  • 2,193
  • 1
  • 18
  • 19
  • 2
    Thanks Jason, I'm gonna mark this as correct answer but I've just disappointed how the heck Android doesn't provide a built-in function such basic work. I just want to filer file extensions ... – Nande kore Dec 11 '14 at 08:42
  • 1
    I too was surprised by this, and was expecting an Intent to handle it. I've face similar issues when wanting to present users with images and let them choose multiples, etc. Perhaps someone will pop up here with a better strategy. – JASON G PETERSON Dec 11 '14 at 08:49
  • @JASONGPETERSON This is what I exactly needed. But I'm not getting how to read `dir` . i tried `Environment.getExternalStorageDirectory().getPath()` but it's not working. I need to find `.srt` file through all folders in storage directory and list them. Kindly help me on this. –  Aug 28 '20 at 05:06
2

A very neat way to do this will be using MaterialFilePicker.

repositories {
    jcentre()
}

dependencies {
    implementation 'com.nbsp:materialfilepicker:1.9.1'
}

I am using it to filter .csv files so,

new MaterialFilePicker()
    .withActivity(activity)
    .withCloseMenu(true)
    .withFilter(Pattern.compile(".*\\.(csv)$"))
    .withFilterDirectories(false)
    .withTitle("Choose File to Import")
    .withRequestCode(REQUEST_CHOOSE_FILE_TO_IMPORT)
    .start();

Now you can easily grab the result in onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == REQUEST_CHOOSE_FILE_TO_IMPORT && resultCode == RESULT_OK) {
        String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
        // Do anything with file
    }
}
Utsav Barnwal
  • 985
  • 11
  • 14
  • Very nice file picker! Opposed to the Uri you normally find in ```data.getData```, you can convert the path to a Uri like so: ```Uri fileUri = Uri.fromFile(new File(filePath));``` – Aldinjo May 05 '21 at 07:47
  • I like this, but it does need min SDK 19... – Serj Sagan Feb 07 '22 at 06:38
0

look at this file picker, the best I found:

https://github.com/Angads25/android-filepicker enter image description here

batsheva
  • 2,175
  • 1
  • 20
  • 32