10

So I'm trying to allow the user to pick a particular piece of media with my Android Application using the method described here: 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!

Community
  • 1
  • 1
FunnyLookinHat
  • 569
  • 1
  • 6
  • 13
  • To add insult to injury - when I use the method with Video - it won't actually allow the user to pick the media - it just plays it when they select it - and it won't show thumbnails. Am I going to be rewriting Gallery ? Is there a way I can call up Gallery and then have it return the Uri for a selected piece of media??? – FunnyLookinHat Dec 09 '09 at 06:09

4 Answers4

17

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()

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
Robert
  • 322
  • 1
  • 3
  • 7
  • 2
    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 Jul 19 '10 at 21:59
  • 3
    Hi! I'm using android 1.6 and it is NOT works! Only image or only videos are okay. Any idea? – narancs Jun 23 '11 at 15:03
  • 1
    So why was this answer accepted? It doesn't work!!! (stated by the person who asked the question) – AlikElzin-kilaka Nov 08 '11 at 09:46
  • 1
    Should it be image/*? Actually I just use "*/*" if you want both image and videos. – dongshengcn Sep 11 '12 at 19:18
2

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
  • 569
  • 1
  • 6
  • 13
  • 1
    The question was about accessing videos AND photos - not just videos. The answer you gave regards only video. – AlikElzin-kilaka Jul 30 '11 at 19:34
  • Unfortunately you can't do that in Android 2.0+ - the faked method of mediaChooser.setType("video/*, images/*"); will not work, and unless you create your own chooser activity that scans for media and returns it to a picker screen - the best you'll get is to give the user a choice between either an Image of Video. – FunnyLookinHat Nov 06 '11 at 21:32
  • Don't know what they've done in 4.2.x, but choosing multiple mime types seems to work just fine :) – Ja͢ck Apr 18 '13 at 04:09
1

Try this

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, 101);
sandy
  • 3,311
  • 4
  • 36
  • 47
1
intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 1);
michkra
  • 81
  • 1
  • 1