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