0

I've to create a file picker which will enable me to select files of specific types such as "pdf", "ppt", "odt" and some more. For this I created a FilteredFilePickerFragment as given here. Now I need to use this fragment but I don't know how.

Here is my Intent in the MainActivity:

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

    // Set these depending on your use case. These are the defaults.
    i.putExtra(FilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
    i.putExtra(FilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
    i.putExtra(FilePickerActivity.EXTRA_MODE, FilePickerActivity.MODE_FILE);

    // Configure initial directory by specifying a String.
    // You could specify a String like "/storage/emulated/0/", but that can
    // dangerous. Always use Android's API calls to get paths to the SD-card or
    // internal memory.
    i.putExtra(FilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());

    startActivityForResult(i, FILE_CODE);

And here is my filtered fragment

    import android.support.annotation.NonNull;

import com.nononsenseapps.filepicker.FilePickerFragment;

import java.io.File;

public class FilteredFilePickerFragment extends FilePickerFragment {

    // File extension to filter on
    private static final String EXTENSION = ".pdf";

    /**
     *
     * @param file
     * @return The file extension. If file has no extension, it returns null.
     */
    private String getExtension(@NonNull File file) {
        String path = file.getPath();
        int i = path.lastIndexOf(".");
        if (i < 0) {
            return null;
        } else {
            return path.substring(i);
        }
    }

    @Override
    protected boolean isItemVisible(final File file) {
        if (mode == MODE_FILE || mode == MODE_FILE_AND_DIR) {
            return EXTENSION.equalsIgnoreCase(getExtension(file));
        }
        return isDir(file);
    }
}
Divyanshu Maithani
  • 13,908
  • 2
  • 36
  • 47

2 Answers2

2

Basically the activity in which you are using Intent shoould intent yo a new activity (URL:https://github.com/spacecowboy/NoNonsense-FilePicker/blob/master/sample/src/main/java/com/nononsenseapps/filepicker/sample/ImagePickerActivity.java)

The above intent creates a fragment call AbstractFilePickerFragment fragment = new ImagePickerFragment();

Imaqe Picker Fragment (https://github.com/spacecowboy/NoNonsense-FilePicker/blob/master/sample/src/main/java/com/nononsenseapps/filepicker/sample/ImagePickerFragment.java) which is a class that extends File Picker Fragment

That is all thats required to shows only certain extension files (may be .doc,.docx, .pdf)

2

If you check the source for FilePickerActvity class you could findout how to implement the fragment.

A custom class which extends FilePickerActivity class.

@SuppressLint("Registered")
public class ImageFilePickerActivity extends AbstractFilePickerActivity<File> {

public ImageFilePickerActivity() {
    super();
}

@Override
protected AbstractFilePickerFragment<File> getFragment(
        @Nullable final String startPath, final int mode, final boolean allowMultiple,
        final boolean allowCreateDir, final boolean allowExistingFile,
        final boolean singleClick) {
    AbstractFilePickerFragment<File> fragment = new ImageFilePickerFragment();
    // startPath is allowed to be null. In that case, default folder should be SD-card and not "/"
    fragment.setArgs(startPath != null ? startPath : Environment.getExternalStorageDirectory().getPath(),
            mode, allowMultiple, allowCreateDir, allowExistingFile, singleClick);
    return fragment;
}
}

ImageFilePickerFragment is similar to your FilteredFilePickerFragment class.

public class ImageFilePickerFragment extends FilePickerFragment {

/**
 *
 * @param file
 * @return The file extension. If file has no extension, it returns null.
 */
private String getExtension(@NonNull File file) {
    String path = file.getPath();
    int i = path.lastIndexOf(".");
    if (i < 0) {
        return null;
    } else {
        return path.substring(i);
    }
}

@Override
protected boolean isItemVisible(final File file) {
    boolean ret = super.isItemVisible(file);
    if (ret && !isDir(file) && (mode == MODE_FILE || mode == MODE_FILE_AND_DIR)) {
        String ext = getExtension(file);
        return ext != null && (".jpg".equalsIgnoreCase(ext)||".jpeg".equalsIgnoreCase(ext)||".png".equalsIgnoreCase(ext));
    }
    return ret;
}

}

Code to run the activity.

Intent i = new Intent(getBaseContext(), ImageFilePickerActivity.class);
            // This works if you defined the intent filter
            // Intent i = new Intent(Intent.ACTION_GET_CONTENT);

            // Set these depending on your use case. These are the defaults.
            i.putExtra(ImageFilePickerActivity.EXTRA_ALLOW_MULTIPLE, false);
            i.putExtra(ImageFilePickerActivity.EXTRA_ALLOW_CREATE_DIR, false);
            i.putExtra(ImageFilePickerActivity.EXTRA_MODE, ImageFilePickerActivity.MODE_FILE);

            // Configure initial directory by specifying a String.
            // You could specify a String like "/storage/emulated/0/", but that can
            // dangerous. Always use Android's API calls to get paths to the SD-card or
            // internal memory.
            i.putExtra(ImageFilePickerActivity.EXTRA_START_PATH, Environment.getExternalStorageDirectory().getPath());

            startActivityForResult(i, RESULT_LOAD_IMAGE);
Sely Lychee
  • 312
  • 1
  • 9