Pick Folder Intent

One of the powerful features about Android vs other mobile operating systems is the ability to create and Intent and ask the system if there are other activities that might be able to respond, even if they are in some other app. Why reinvent the wheel with every app if there is already one much more capable already out there? By apps working together, we can create an ecosystem of apps that inter-operate without requiring explicit code shared between them. Intents are powerful in that they are open ended and can carry a vast array of data and filters between apps. However, since there is no strict definition on how to create intents for all kinds of purposes, one must follow published guidelines from the authors of apps or rely on a site like OpenIntents.org that register Intents so others may use them correctly.

I tried to make AttachSave use a couple of different published intents so that if one fails to find any app, then the other may find an app increasing the chances of finding a compatible activity. The first intent I try uses the Intent.PICK action and the second intent I try uses the custom action “org.openintents.action.PICK_DIRECTORY”.

/**
 * OpenIntents action for picking a folder
 */
public static final String ACTION_PICK_DIRECTORY = "org.openintents.action.PICK_DIRECTORY";
protected static final int RC_PICK_FOLDER = Activity.RESULT_FIRST_USER; //or anything higher

private void pickFolder(File aStartFolder) {
    Uri theUri = new Uri.parse("folder://"+aStartFolder.getPath());
    Intent theIntent = new Intent(Intent.ACTION_PICK);
    theIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
    theIntent.setData(theUri);
    theIntent.putExtra(Intent.EXTRA_TITLE,getString(R.string.title_selectfolder));
    try {
        startActivityForResult(theIntent,RC_PICK_FOLDER);
    } catch (ActivityNotFoundException e) {
        theIntent.setAction(ACTION_PICK_DIRECTORY);
        theIntent.setData(Uri.fromFile(aStartFolder));
        try {
            startActivityForResult(theIntent,RC_PICK_FOLDER);
        } catch (ActivityNotFoundException anfe) {
            //display error or ask user to download an app
        }
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent aData) {
    switch (requestCode) {
        case RC_PICK_FOLDER:
            if (resultCode==RESULT_OK && aData!=null && aData.getData()!=null) {
                String theFolderPath = aData.getData().getPath();
                // DO SOMETHING WITH THE PATH
            }
            break;
    }
}

By using an Intent, I provide a feature I do not have to actually code into my app. I also put in code so that if it fails to find any app capable of handling the intent, then it will ask the user if they wish to download one in particular. If they do not wish to download another app, they can just as easily type in the folder name and path to a provided EditText widget, it just won’t be as simple as popping up some other app dedicated to such things.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>