Aymon Fournier Aymon Fournier - 8 months ago 76
Android Question

Choose File Dialog

Does anyone know of a complete choose file dialog? Maybe one where you can filter out all files except for ones with specific extensions?

I have not found anything lightweight enough to implement easily into one of my projects. The only other option seems to be using OI FileManager's open intents, but that requires the user already having the file manager installed.

I would be grateful if someone could point out a Dialog that would allow the user to browse folders and select a file, and return the path.

Answer Source

You just need to override onCreateDialog in an Activity.

//In an Activity
private String[] mFileList;
private File mPath = new File(Environment.getExternalStorageDirectory() + "//yourdir//");
private String mChosenFile;
private static final String FTYPE = ".txt";    
private static final int DIALOG_LOAD_FILE = 1000;

private void loadFileList() {
    try {
    catch(SecurityException e) {
        Log.e(TAG, "unable to write on the sd card " + e.toString());
    if(mPath.exists()) {
        FilenameFilter filter = new FilenameFilter() {

            public boolean accept(File dir, String filename) {
                File sel = new File(dir, filename);
                return filename.contains(FTYPE) || sel.isDirectory();

        mFileList = mPath.list(filter);
    else {
        mFileList= new String[0];

protected Dialog onCreateDialog(int id) {
    Dialog dialog = null;
    AlertDialog.Builder builder = new Builder(this);

    switch(id) {
        case DIALOG_LOAD_FILE:
            builder.setTitle("Choose your file");
            if(mFileList == null) {
                Log.e(TAG, "Showing file picker before loading the file list");
                dialog = builder.create();
                return dialog;
            builder.setItems(mFileList, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    mChosenFile = mFileList[which];
                    //you can do stuff with the file here too
    dialog = builder.show();
    return dialog;