Handling “Browse for Folder” intents

If you are an Android app developer interested in responding to a “pick folder” intent, this article is for you. There is no strict definition on how to respond to intents and so one must follow published guidelines from the author of each app.

Method 1: Action: android.intent.action.PICK, Scheme: folder

This kind of Intent action has been registered with OpenIntents.org. It uses a generic PICK action using a “folder” scheme:

  • Action = Intent.ACTION_PICK
  • DataUri = “folder://{path to default folder}”
  • ExtraString, Key=Intent.EXTRA_TITLE, Value={string used as alternate app title}

I chose to use this Intent because I believe the combination of Actions and Schemes together should be the determining factor on what it is you wish to pick rather than defining a different action for all the various kinds of data you want to pick. In order to write an activity to respond to this Intent, define your activity in AndroidManifest.xml and give it an Intent filter that will respond to an ACTION_PICK with a “folder” scheme, such as:

<activity android:exported="true" android:name="MyBrowseFolderActivity">
    <intent-filter>
        <action android:name="android.intent.action.PICK"></action>
        <category android:name="android.intent.category.DEFAULT"></category>
        <data android:scheme="folder"></data>
    </intent-filter>
</activity>

Once you have the AndroidManifest.xml properly defined, check for the Intent in your onCreate method of the activity. One such way of checking would be to call a method like the following:

private void processExternalRequest() {
    String theAction = null;
    String theScheme = null;
    if (getIntent()!=null) {
        theAction = getIntent().getAction();
        theScheme = getIntent().getScheme();
    }
    if (theAction!=null && theScheme!=null &&
            theAction.equals(Intent.ACTION_PICK) && theScheme.equalsIgnoreCase("folder")) {
        //pick a folder intent
        if (aIntent.getData()!=null && aIntent.getData().getPath()!=null) {
            File theDefaultFolder = new File(aIntent.getData().getPath());
        }
        if (aIntent.getStringExtra(Intent.EXTRA_TITLE)!=null) {
            setTitle(aIntent.getStringExtra(Intent.EXTRA_TITLE));
        }
        /*
         * SETUP APP TO PICK A FOLDER USING theDefaultFolder AS A STARTING POINT
         */
    }
}

At some point, your activity will be ready to finish and pass back the picked folder path. You would do that by writing a few lines and then calling finish(). It is recommended that you use getIntent() instead of creating a new intent since the calling Activity may have some extra data inside the one it sent you that it needs and is expecting on the return.

Intent theIntent = getIntent();
theIntent.setData(Uri.fromFile(thePickedFolder));
setResult(RESULT_OK,theIntent);
finish();

Method 2: Action: org.openintents.action.PICK_DIRECTORY, Scheme: file

OpenIntents.org has been around for quite a while trying to catalog various Intents and hopefully provide enough flexibility to avoid having to create a custom Intent for your app. Please see their site on examples and usage information. It is very similar to Method 1, except you do not have to worry about the Scheme and so you do not have to check it, nor specify it in your Intent Filter. You will, instead, have to specify the “file” as your scheme if you decide to implement this Intent.

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>