How do I allow a user to browse/choose a file for my app use in Android?

android, browser, file

Solution

Use Intent.ACTION_GET_CONTENT to launch an activity for the user to select the desired media type. For selecting an image, you would probably want the MIME type of "image/*". You also want to wrap this in a choose since often there will be multiple content sources for the user to select from (for example they could browse through the gallery, or could take a picture at that point, or a generic file browser if one is installed, etc).

Here is some rough code, probably buggy because I am just writing it out here:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");

// Do this if you need to be able to open the returned URI as a stream
// (for example here to read the image data).
intent.addCategory(Intent.CATEGORY_OPENABLE);

Intent finalIntent = Intent.createChooser(intent, "Select profile picture");

startActivityForResult(finalIntent, IMAGE_SELECTED);

When the user has selected something, you will get the selection back in onActivityResult().

More reference: http://developer.android.com/reference/android/content/Intent.html#ACTION_GET_CONTENT

Problem

I have an app where the user can choose a profile pic using images stored on the phone or SD. Do I have to write my own file explorer for this? I've seen a couple examples on anddev, but wasn't sure if there is another way.

Original source