Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 602
Jim D. Avatar asked Jan 22 '23 19:01

Jim D.


2 Answers

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

like image 184
hackbod Avatar answered Jan 25 '23 08:01

hackbod


The OI File Manager control is one excellent solution

The OpenIntents file manager allows you to browse your SD card, create directories, rename, move, and delete files. It also acts as an extension to other applications to display "Open" and "Save" dialogs.

like image 36
jspcal Avatar answered Jan 25 '23 09:01

jspcal