views:

612

answers:

3

So I'm trying to allow the user to pick a particular piece of media with my Android Application using the method described here: http://stackoverflow.com/questions/550905/access-pictures-from-pictures-app-in-my-android-app

It works great, except for the fact that I can seemingly only choose between either Video or Photo to present the user with, not both at the same time. Is there a good way to do this with:

startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE);

Thanks!

+3  A: 

I've used this several times. The best way is something like:

Intent mediaChooser  new Intent(Intent.ACTION_GET_CONTENT);
//comma-separated MIME types
mediaChooser.setType("video/*, images/*");
startActivityForResult(mediaChooser, 1);

Even if this isn't perfectly accurate, it has worked fine in everything I've used it in. It will open up a Gallery-esque activity with a thumbnail list of every picture/video in the user's gallery. The returned intent to onActivityResult() has an extra called "DATA" which will be a content:// URI to the selected media.

EDIT: oops, to get the URI to the selected media you actually want to call getData() on the Intent that gets passed to onActivityResult()

Robert
Robert - For some reason whenever I use the following line I have an empty picker show up:mediaChooser.setType("video/*, images/*");I've also tried this with no luck...mediaChooser.setType("video/*, image/*");However, each one individually works fine... i.e.mediaChooser.setType("video/*");mediaChooser.setType("image/*");Could you paste an exact code snippet that works?**** EDIT - Seems like this doesn't work anymore post version 2.0
FunnyLookinHat
A: 

I am actually trying to do the same thing, though I only want to access videos. But I am already stuck at the very beginning. If you would be so kind to show me what you have done, that would be absolutely great :)

kivy
See my answer below... needed more space...
FunnyLookinHat
AWESOME, THANKS :)
kivy
A: 

Kivy - The easiest way is to create an intent to select a piece of media and restrict it to video:

Intent pickMedia = new Intent(Intent.ACTION_GET_CONTENT);
pickMedia.setType("video/*");
startActivityForResult(pickMedia,12345);

Note - 12345 is the integer that your app needs to listen for on a request callback so that you can send whatever info you receive wherever you need to.

You then need to also have that same activity listening for the info to be sent back from that intent:

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 12345) {
            if (resultCode == Activity.RESULT_OK) {
                Uri selectedVideoLocation = data.getData();

                // Do something with the data...
            } 

        }
    }

Cool?

FunnyLookinHat