1

I want users to be able to choose a photo from their media library but don't want them to be able to choose a video. I'm having trouble limiting them to only choosing a photo. When I open the gallery app using

new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)

then they are able to choose both photos and videos. Is there a way to limit them to only choosing photos?

lowellk
  • 2,049
  • 4
  • 21
  • 26
  • 1
    this post says its impossible without mimes but he doesnt quote documentation. http://stackoverflow.com/questions/4991464/retrieving-only-images-from-gallery-when-firing-android-image-chooser-intent look here as well though http://stackoverflow.com/questions/2507898/how-to-pick-a-image-from-gallery-sd-card-for-my-app-in-android – owen gerig Nov 21 '11 at 21:15

2 Answers2

5

For that you need to also set a mime type to "image/*" using Intent.setType(String type).

UPDATE: Looks like the proper way to set Uri and mime type at once is:

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");

UPDATE 2: This is because when we set mime type or data uri separatelly look what happens (taken from Intent sources):

public Intent setType(String type) {
    mData = null;
    mType = type;
    return this;
}

public Intent setData(Uri data) {
    mData = data;
    mType = null;
    return this;
}

At least this is true on API 2.2.

Vit Khudenko
  • 28,288
  • 10
  • 63
  • 91
1

Another option would be to use ACTION_GET_CONTENT instead of ACTION_PICK. It allows you to set a the MIME type you want without having to specify a URI at all.

aganders3
  • 5,838
  • 26
  • 30