81

Is there a way to use intent.setType() and supply multiple broad types (like images and video)?

I am using an ACTION_GET_CONTENT. It seems to be working with just comma-separated types.

Jeff Axelrod
  • 25,625
  • 29
  • 137
  • 239
James
  • 811
  • 1
  • 6
  • 4

6 Answers6

133

In Android 4.4 when using the Storage Access Framework you can use the EXTRA_MIME_TYPES to pass multiple mime types.

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("*/*");
String[] mimetypes = {"image/*", "video/*"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, REQUEST_CODE_OPEN);
Fred
  • 10,269
  • 4
  • 48
  • 63
20

Actually, multiple mime-types are supported. Have you even tried it???

For example: intent.setType("image/*,video/*") will display photos and videos...

For me it works. It should work for you too...

[EDIT]: This works partially, as not all the gallery apps choose to implement support for multiple mime types filters.

bazyle
  • 705
  • 6
  • 13
  • 4
    This solution is working... but only with applications that allows two mime-types. The default one "Gallery" doesn't. – Corbella Nov 27 '13 at 08:50
  • 1
    Actually, it depends on the "Gallery" application you have installed. There are different versions of them being distributed with different android ROMs. – bazyle Nov 27 '13 at 20:03
  • Works for me in minor cases only. Same images may or may not be selectable. Android 5.0.1 – Ingweland Apr 07 '15 at 21:39
2

Sorry, this is not currently supported. You have two options:

(1) Use a MIME type of */* and accept that there may be some things the user can pick that you won't be able to handle (and have a decent recovery path for that); or

(2) Implement your own activity chooser, doing direct calls on the package manager to get the activities that can handle both MIME types for the intent, merging those lists, and displaying them to the user.

Also, setType() does not work with comma-separated types at all. It must be one and only one MIME type.

Jeff Axelrod
  • 25,625
  • 29
  • 137
  • 239
hackbod
  • 88,517
  • 16
  • 135
  • 152
  • 3
    Actually, multiple mime-types are supported. Have you even tried it??? For example: image/*,video/* will display photos and videos... – bazyle Jul 25 '13 at 19:36
  • @vchelbanster Multiple mimetypes are not supported in all devices. With some android version like < 5.0, it would pick only first mimetype. – Shivam Pokhriyal Dec 20 '18 at 11:04
  • @ShivamPokhriyal Partially agree - it's not available for all the apps. Since the app that gets a composite intent type needs to handle multiple mime types for this to work, not all the apps (like gallery app) will work, which might create the impression that it's the OS version that's dependent on. In fact, it's the app's creator that need to handle multiple mime types. So yeah - it's limited support. See my answer. – bazyle Mar 19 '19 at 01:23
  • @vchelbanster Yup, at first it appeared to me that this is related to OS version. Later I figured out that if your device doesn't have app that handles such type of intents, then the problem will occur. – Shivam Pokhriyal Mar 19 '19 at 06:40
2

For me what worked best was:

intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);


You can add several mime types like this

intent.setType("image/*|application/pdf|audio/*");

But the intent chooser will only display applications that can handle images because it is the first in the mime type string.

However if you have a file manager installed (I tested with the CyanogenMod file manager) you can choose a file that is audio or pdf or an image.

If the audio mime type is the first one, like this:

intent.setType("audio/*|image/*|application/pdf");

The intent chooser will display only applications that handle audio.
Again using the file manager you can select an image or pdf or audio.

Raimundo
  • 553
  • 5
  • 20
0

you can pass multiple mime types if you separate with |

Intent.setType("application/*|text/*");
Cifus
  • 87
  • 7
-3

for my work with semicolons.

Example:

intent.setType("image/*;video/*")

or

sIntent.putExtra("CONTENT_TYPE", "image/*;video/*"); 
javier
  • 3
  • 1
  • 1
    Instead of the hard-coded "CONTENT_TYPE" string you can use the `Intent.EXTRA_MIME_TYPES` constant. Also instead of semicolons, you can pass an array of strings. – Fred Jan 16 '19 at 08:58