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.
Asked by: Patrick771 | Posted: 20-01-2022
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_CONTENTAnswered by: Carina462 | Posted: 21-02-2022
The OI File Manager control is one excellent solution
Answered by: Adrian107 | Posted: 21-02-2022
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.
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android