'How do I prevent Intent.createChooser from using the Intent automatically if there is only one intent?

I'm trying to create an Intent Chooser for editing a "Profile Picture", with the following options: 1. Camera 2. Gallery 3. Delete Image

Based on the permissions supplied by the user, I am appending cameraIntent & galleryIntent to a list of intents that I add to to the chooser. However, if the user denies both camera & gallery permissions, the chooser automatically falls back to option 3, Delete Image, which is not what I want.

    public Intent getImageChooserIntent(){
            Intent galleryIntent = getGalleryIntent();
            Intent cameraIntent = getCameraIntent();
            Intent imageSourceChooser = Intent.createChooser(new Intent(), "Select Source");
            List<Intent> intentList = new ArrayList<>();
            if (galleryIntent != null) {
                intentList.add(galleryIntent);
            }
            if (cameraIntent != null) {
                intentList.add(cameraIntent);
            }

            intentList.add(new Intent(context, DeleteImageActivity.class));
            imageSourceChooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Intent[0]));
            return imageSourceChooser;

        }

I expect the prompt to show a chooser with just one option - "Delete Image", if the user denies permission for both camera & gallery.



Solution 1:[1]

That's not the way it works though. In your case, if you know the user hasn't granted permission for camera or gallery, you can just show your own dialog asking the user if they want to delete the image.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Saket