73

I'm trying to let the user choose any image that they want on their device to use as a wallpaper in this wallpaper application I'm building. For some reason when I write:

Intent myIntent = new Intent(Intent.ACTION_PICK);
myIntent.setType("image/*");
startActivityForResult(myIntent, 100);

I go straight into the gallery, but when I write:

Intent myIntent = new Intent(Intent.ACTION_GET_CONTENT, null);
myIntent.setType("image/*");
startActivityForResult(myIntent, 100);

I get to choose from Gallery, or Google Drive. What is the best way to let the user choose what app to retrieve the picture from every time? Or why do those two different intent constants make a difference?

rlazo
  • 635
  • 4
  • 14
EGHDK
  • 17,818
  • 45
  • 129
  • 204

3 Answers3

101

Your first Intent is invalid. The protocol for ACTION_PICK requires you to supply a Uri indicating the collection you are picking from.

What is the best way to let the user choose what app to retrieve the picture from every time?

If you want the user to choose something based on MIME type, use ACTION_GET_CONTENT.

If you have some specific collection (identified by a Uri) that you want the user to pick from, use ACTION_PICK.

In case of a tie, go with ACTION_GET_CONTENT. While ACTION_PICK is not formally deprecated, Dianne Hackborn recommends ACTION_GET_CONTENT.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 5
    Interesting that the first intent does work, although it's invalid. Appreciate the pointers though. Thanks – EGHDK Jul 20 '13 at 23:56
  • 2
    Out of curiosity, why does Dianne make it sound like `ACTION_GET_CONTENT` was added later to the API, calling it "modern". They were both available in API 1, so this makes me feel like the Android engineers goofed up if they really didn't want people using `ACTION_PICK`. Was it too late to remove that by API 1? – Tony Chan Oct 30 '13 at 02:34
  • 1
    @Turbo: You'd have to ask her, sorry. – CommonsWare Oct 30 '13 at 10:51
  • 4
    @CommonsWare How did you gained the power to answer every deep question in a simple way. ? ;) – theapache64 Nov 02 '16 at 02:39
  • `Intent.ACTION_PICK` doesn't seem to be invalid as it is still working (verified) and it is still being recommended to be used for file sharing in Android. Please see https://developer.android.com/training/secure-file-sharing/share-file and https://developer.android.com/training/secure-file-sharing/request-file – petrsyn Mar 29 '20 at 22:22
  • 1
    @petrsyn: I did not say that `ACTION_PICK` was invalid. I said that the `Intent` was invalid, because it did not specify a `Uri` of the collection to pick from. This is covered in [the documentation for `ACTION_PICK`](https://developer.android.com/reference/android/content/Intent.html#ACTION_PICK) and [the documentation for `ACTION_GET_CONTENT`](https://developer.android.com/reference/android/content/Intent.html#ACTION_GET_CONTENT). The 2nd documentation link has a broken example; I have filed [an issue](https://issuetracker.google.com/issues/152696757) to try to get that example fixed. – CommonsWare Mar 29 '20 at 22:52
  • @CommonsWare Thanks for the effort to file an issue report. In either case, the Google example worked for me even without specifying the URI for the input Intent using the ACTION_PICK. I'm curious about the issue report result. – petrsyn Mar 30 '20 at 23:38
  • The way ACTION_PICK works has changed but the official docs has not been updated (I dont know why). You can see in this official example https://developer.android.com/guide/components/intents-common#Contacts that setting URI is not required and setting type works. – Ravi Raj Apr 29 '21 at 16:59
15

The modern action is ACTION_GET_CONTENT, which is much better supported,

ACTION_PICK :

Activity Action: Pick an item from the data, returning what was selected.

Input: getData() is URI containing a directory of data (vnd.android.cursor.dir/*) from which to pick an item.

Output: The URI of the item that was picked.

Constant Value: "android.intent.action.PICK"


Difference :-

Activity Action: Allow the user to select a particular kind of data and return it.

This is different than ACTION_PICK in that here we just say what kind of data is desired, not a URI of existing data from which the user can pick.

A ACTION_GET_CONTENT could allow the user to create the data as it runs (for example taking a picture or recording a sound), let them browse over the web and download the desired data, etc.

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

Community
  • 1
  • 1
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
  • 2
    "ACTION_PICK is deprecated" -- technically, it's not. – CommonsWare Jul 20 '13 at 18:33
  • I couldn't tell the difference between ACTION_PICK and ACTION_GET_CONTENT until I read your example: taking a picture or recording a sound. Thanks. – jclova Nov 28 '14 at 22:00
  • when i am using action_get_content it opens all the folders when i select image from external memory card and then upload the image to server filenotfound exception is coming and the select same image from gallery it is uplaoding to server .please give me the solution.. – Ajay Apr 28 '16 at 12:16
-2
 public static final String ACTION_GET_CONTENT

Added in API level 1

Activity Action: Allow the user to select a particular kind of data and return it. This is different than ACTION_PICK in that here we just say what kind of data is desired, not a URI of existing data from which the user can pick. A ACTION_GET_CONTENT could allow the user to create the data as it runs (for example taking a picture or recording a sound), let them browse over the web and download the desired data, etc.

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

Community
  • 1
  • 1
Kevin
  • 2,605
  • 2
  • 20
  • 15