Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get number of activities that satisfy implicit intent?

I'm using implicit intent to, say, open Gallery to pick an image. I'm normally doing the following:

Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
    .takeIf { it.resolveActivity(activity.packageManager) != null }
    ?.let {
        val intent = Intent.createChooser(it, "Choose gallery")
        activity.startActivityForResult(intent, rc)
    }

But if that intent is resolved by just single application on my device, and there is no other gallery app but one, then chooser will only display single choice which is useless.

How could I figure out whether there are many activities that could handle that intent? How to get the number of activities that actually fit? And hence, decide whether to show chooser or not.

like image 689
Maxim Alov Avatar asked Jan 28 '26 19:01

Maxim Alov


1 Answers

How could I figure out whether there are many activities that could handle that intent?

Call queryIntentActivities() on a PackageManager, supplying your Intent.

How to get the number of activities that actually fit?

Sorry, but I do not know what "fit" means in this context.

And hence, decide whether to show chooser or not

You could simply get rid of the Intent.createChooser() call. You only use that when you wish to force the user to have to deal with a chooser. If you leave Intent.createChooser() off, the system will show a chooser on its own when needed. That occurs if:

  • There are 2+ activities that match the Intent, and
  • The user has not chosen a default activity for that Intent structure

BTW, please remove type = "image/*" from your code. ACTION_PICK does not use a MIME type.

like image 60
CommonsWare Avatar answered Jan 31 '26 07:01

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!