Search code examples
androidkotlinandroid-manifestandroid-11

Use resolveActivity To Check if Intent Can Be Handled


I am trying to disable the camera launch button in my app if the camera intent cannot be handled by the device. I am targeting Android R (11). The code that I have so far is this

  val packageManager: PackageManager = requireActivity().packageManager

  val captureImage = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
  if (captureImage.resolveActivity(packageManager) == null) {
    isEnabled = false
  }

The issue that I am having is that when I implement this code the camera launch button is always disabled even on devices that support it. I am not sure why it is doing this other than that maybe I need to add package-visibility into the manifest file but I am unsure how that all works as the documentation isn't the best when it comes to examples. Any help or guidance would be greatly appreciated.


Solution

  • You can add queries like this in your manifiest

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="app.android">
    
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.INTERNET" />
    
        <queries>
            <package android:name="com.whatsapp" />
    
            <intent>
                <action android:name="android.media.action.IMAGE_CAPTURE" />
            </intent>
            <intent>
                <action android:name="android.intent.action.DIAL" />
            </intent>
        </queries>
    
         <application
          ...
         </application>
    </manifest>