How to allow the user to view / select a file for my Android application?

I have an application in which the user can select the pic profile using images stored on the phone or SD.

Should I write my own file explorer for this?

I saw a couple of examples on anddev, but was not sure if there was another way.

+3
source share
2 answers

Use Intent.ACTION_GET_CONTENT to trigger an action for the user to select the desired media type. To select an image, you probably need the MIME type "image / *". You also want to wrap this up with a choice, since often you will have several sources of content for the user (for example, they can view the gallery or take a picture at this point or a common file browser, if installed, etc.).

Here is some crude code, possibly a buggy, because I'm just writing it 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);

-, onActivityResult().

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

+6

OI File Manager -

OpenIntents SD-, , , . "Open" "".

+1

Source: https://habr.com/ru/post/1727881/


All Articles