0

I am using following code to pick an image from the gallery. When I test it with Samsung Galaxy S4, it directly goes to Gallery that is what I want actually.

BUT, when I test my code on LG Optimus II device, it shows a dialog gives an option to choose either Gallery or Picture. In other words, it adds one more layer which I do not want.

Both devices have KitKat 4.4.2 operating system.

public static void showFileChooser(Activity activity) {
  Intent intent = new Intent();
  intent.setType("image/*");
  intent.setAction(Intent.ACTION_PICK);
  activity.startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);
}
casillas
  • 16,351
  • 19
  • 115
  • 215

2 Answers2

5

when I test my code on LG Optimus II device, it shows a dialog gives an option to choose either Gallery or Picture

That is because there are two activities on that device that support ACTION_PICK of image/* files. There can be zero to N such activities, depending on what apps are on the device. This will include both pre-installed apps and apps that the user installed themselves. These will range from local file managers to general cloud providers (e.g., Dropbox) to image-specific apps (e.g., Instagram).

In other words, it adds one more layer which I do not want.

Then do not use ACTION_PICK. You are delegating to a third-party app; it is up to the user, not you, what third-party app the user wishes to use.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Thanks a lot for the clarification. What Intent do you recommend me to open directly Gallery? Is it possible that user does not have neither gallery or picture? If yes, then what intent needs to be called to be on safe side ? In general, I would like to know what is the common approach to handle this situation. – casillas Feb 19 '16 at 16:13
  • 1
    @casillas: "what Intent do you recommend me to open directly Gallery?" -- there is none. First, there is no requirement that an Android device have an app named "Gallery". Even if a device has such an app, there is no standard `Intent` that will work across all devices that will specifically open that app and that app alone. Even if there were such an `Intent`, most likely it would just open the app, not provide you with any sort of "pick" functionality. And, the *user* may not want to use that app. "Is it possible that user does not have neither gallery or picture?" -- unlikely, but possible. – CommonsWare Feb 19 '16 at 16:17
  • Thanks a lot one more time for further detailed information. What is the common approach to pick an image ? I am working on an application where user selects images and once he submits then images are sent to server. – casillas Feb 19 '16 at 16:19
  • @casillas: "What is the common approach to pick an image ?" -- `ACTION_GET_CONTENT` with `image/*` would be a bit more common, though you will get the same behavior, where the *user* has the freedom to choose images from where the *user* chooses, because it is the *user's* device, the *user's* images, and the *user's* available apps for working with those images. – CommonsWare Feb 19 '16 at 16:26
  • Thanks a lot CommonsWare, for the detailed information. – casillas Feb 19 '16 at 16:27
3

if you want Gallery then

    Intent pickImageIntent = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

please use for api 19 or above

if (Build.VERSION.SDK_INT < 19) {
                        Intent intent = new Intent();
                        intent.setType("image/jpeg");
                        intent.setAction(Intent.ACTION_GET_CONTENT);

                        startActivityForResult(Intent.createChooser(intent,
                                "Select image to promote"),
                                GALLERY_INTENT_CALLED);

                    } else {
                        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
                        intent.addCategory(Intent.CATEGORY_OPENABLE);
                        intent.setType("image/jpeg");
                        startActivityForResult(intent,
                                GALLERY_KITKAT_INTENT_CALLED);
                    }
Sree Reddy Menon
  • 1,301
  • 13
  • 23
  • But my app should be compatible for `Kitkat`, `Lollillop` and `Marshmallow` as well. The targeted lowest API is 15 – casillas Feb 19 '16 at 15:56