Learn how to configure the AndroidFilePicker library by rosuH to select only directories using Java.

How to select directories only in rosuH AndroidFilePicker with the Java Implementation

The AndroidFilePicker by rosuH is a very useful file/folder selector library that is constantly evolving with the goal of rapid integration, high customization, and configurability. I've been using it in a personal project where the user should be able to select files and directories from the system. There are a couple of examples in the official documentation here, for example the Kotlin code to launch the picker to select directories is the following one:

    .filter(object : AbstractFileFilter() {
        override fun doFilter(listData: ArrayList<FileItemBeanImpl>): ArrayList<FileItemBeanImpl> {
            return ArrayList(listData.filter { item ->

However, i don't use Kotlin as I'm currently working on the development of plugins using CapacitorJS so I'll stick with Java. In case that you need to implement the same feature of selecting directories only with Java using this library, I will explain you how to easily do it.

1. Implement Filter class

In Kotlin is quite easy to filter items from the array list used to display the elements on the picker. With Java is another history as it requires a little bit more of code. To get started, you will need to create a new Class that extends the AbstractFileFilter class of the filepicker (be sure to replace the package according to yours). In this class, you need to override the doFilter method and write your own custom logic to remove/add items from the list and return it to display it in the picker. In this example, we will simply create a filter that removes files from the picker (showing only directories):

// FilterDirectoryOnly.java
package com.useyourown.package.name;

import java.util.ArrayList;
import me.rosuh.filepicker.bean.FileItemBeanImpl;
import me.rosuh.filepicker.config.AbstractFileFilter;

public class FilterDirectoryOnly extends AbstractFileFilter{

    // Override the doFilter method

    public ArrayList<FileItemBeanImpl> doFilter(ArrayList<FileItemBeanImpl> listData){

        // In this logic, we will return a new list that will contain only the items that we desire
        ArrayList<FileItemBeanImpl> newList = new ArrayList<FileItemBeanImpl>();

        // Iterate over every element of the list
        for (FileItemBeanImpl fileItem: listData) {

            // If the element is a directory, include it

        return newList;

2. Launch FilePicker filtering directories only

To apply the filter, you need to simply provide a new instance of our filter class as first argument of the filter method:

// Launch the filepicker defining as filter
// our recently created class (new FilterDirectoryOnly())
    .filter(new FilterDirectoryOnly())

And that should be enough to launch the filepicker that only allows to select directories (remove the limit of a single folder removing the maxSelectable method).

Happy coding ❤️!

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.