How to check if an intent can be handled from some activity?

AndroidAndroid IntentMime TypesFile TypeIntentfilter

Android Problem Overview

I have this method so far , but it came up like something is missing

for example I have a file /sdcard/sound.3ga that returns false ( like there is no activity that can handle this type of file ) , But when I open it from the file manager it opens with the media player with no problem

I think this intent is not complete and I need to to something more to make my self sure that the handlerExists variable will be false ONLY if there is no activity that can handle this intent

PackageManager pm = getPackageManager();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uriString)).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setDataAndType(Uri.fromFile(new File(uriString)),mimetype);
boolean handlerExists = intent.resolveActivity(pm) != null;

Android Solutions

Solution 1 - Android

edwardxu's solution works perfectly for me.

Just to clarify a bit:

PackageManager packageManager = getActivity().getPackageManager();
if (intent.resolveActivity(packageManager) != null) {
} else {
    Log.d(TAG, "No Intent available to handle action");

Solution 2 - Android

PackageManager manager = context.getPackageManager();
List<ResolveInfo> infos = manager.queryIntentActivities(intent, 0);
if (infos.size() > 0) {
    //Then there is an Application(s) can handle your intent
} else {
    //No Application can handle your intent

Have you tried this intent?

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromFile(yourFileHere));

Solution 3 - Android

if (intent.resolveActivity(getPackageManager()) == null) {
    // No Activity found that can handle this intent. 
    // There is an activity which can handle this intent. 

Solution 4 - Android

You can use:

public static boolean isAvailable(Context ctx, Intent intent) {
   final PackageManager mgr = ctx.getPackageManager();
   List<ResolveInfo> list =
   return list.size() > 0;

Solution 5 - Android

Using Kotlin If you need to do something when intent is not available,

fun isIntentAvailable(context: Context, action: String?): Boolean {
    val packageManager = context.packageManager
    val intent = Intent(action)
    val resolveInfo: List<*> = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
    return resolveInfo.isNotEmpty()

Implement this method as

private const val SAMPLE_INTENT = "com.realwear.barcodereader.intent.action.SCAN_BARCODE"

    //Do Stuff

If you don't have anything to do,

Intent(SAMPLE_INTENT).also { barcodeReaderIntent ->
    barcodeReaderIntent.resolveActivity(packageManager)?.also {
        barcodeReaderIntent.putExtra(EXTRA_CODE_128, false)
        startActivityForResult(barcodeReaderIntent, BARCODE_REQUEST_CODE)

Solution 6 - Android

The Clear Answer to solve this problem

if (intent.resolveActivity(getPackageManager()) != null) {

Solution 7 - Android

Small update for one looking for something similar in 2021 :) Since Android 11 it is not desired to call the package manager, so extra efforts are needed. So why don't you just wrap startActivity() in try-catch? Or even better - use Rx beautiful error handing:

.flatMapCompletable { 
          .onErrorResumeNext { completableCallbackIfNotResolvable() }

Solution 8 - Android

Another approach using kotlin extension

 fun Context.isIntentAvailable(intent: Intent): Boolean {
    val resolveInfo: List<*> = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY)
    return resolveInfo.isNotEmpty()

Usage inside fragment

val available = requireContext().isIntentAvailable(this)

Usage inside activity

val available = this.isIntentAvailable(this)

Solution 9 - Android

Here you are another Kotlin solution. It works perfectly even for Android 30+. First include this in you app manifest:

        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="choose-scheme-goes-here" />

Then you can check if an intent can be handled with this function:

private fun isIntentAvailable(intent: Intent): Boolean {
    val manager = requireContext().packageManager
    val info = manager.queryIntentActivities(intent, 0)
    return info.size > 0


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLukapView Question on Stackoverflow
Solution 1 - AndroidSparky1View Answer on Stackoverflow
Solution 2 - AndroidMohammad ErsanView Answer on Stackoverflow
Solution 3 - AndroidxuxuView Answer on Stackoverflow
Solution 4 - AndroidPedro LobitoView Answer on Stackoverflow
Solution 5 - AndroidVarun ChandranView Answer on Stackoverflow
Solution 6 - AndroidBassam SalamaView Answer on Stackoverflow
Solution 7 - AndroidVadimView Answer on Stackoverflow
Solution 8 - AndroidOscar Emilio Perez MartinezView Answer on Stackoverflow
Solution 9 - AndroidMariusz WiazowskiView Answer on Stackoverflow