14

I am trying to open Gmail from my app ( on click of button) . I am facing issue in android 11 only . Its not opening the Gmail . queryIntentActivities is returning empty list in this case.

Please help me how to fix this issue for android 11. Below is the code which i am using ..

 val pkgManager = aContext.packageManager
            val packages = pkgManager.queryIntentActivities(intent, 0)
            if (!packages.isEmpty()) {
                for (resolveInfo in packages) {
                    val packageName = resolveInfo.activityInfo.packageName
                    aEmailClientNames.add(resolveInfo.loadLabel(aContext.packageManager).toString())
                    aEmailClientIcons.add(resolveInfo.loadIcon(aContext.packageManager))
                    aEmailClientPackageNames.add(packageName)
                }
Preeti Tiwari
  • 297
  • 3
  • 10
  • Did you check [this](https://stackoverflow.com/questions/63246442/android-11-r-return-empty-list-when-querying-intent-for-action-image-capture)? – Piyush Jan 08 '21 at 12:54
  • Yes i checked that one. That is for opening image . They are adding query in manifest file. what query to add in manifest to open gmail app? i tried many but didnt get result. – Preeti Tiwari Jan 08 '21 at 13:06
  • 1
    its working after adding permission – Preeti Tiwari Jan 08 '21 at 13:23
  • 1
    "what query to add in manifest to open gmail app?" -- you need one that matches whatever you are specifying in `intent` in the second line of your code snippet. "its working after adding permission" -- bear in mind that you may be banned from the Play Store, unless you can provide justification to Google for why you need this permission. – CommonsWare Jan 08 '21 at 13:50

2 Answers2

20

packageManager.queryIntentActivities(intent, 0) will return EMPTY list if your app is running on targetSdkVersion 30 to resolved this issue you have to use <queries> in menifest as queryIntentActivities(), are filtered based on the calling app's declaration.

Fix image capture + image upload to work with Android "scoped storage"

The issue can be related to new package visibility (https://developer.android.com/about/versions/11/privacy/package-visibility). After all updates (at least Android Studio 4.1) try to add in your manifest that shows what action is required in app. In my case problem disappears when adds.

IMAGE_CAPTURE for CAMERA, GET_CONTENT for GALLERY (to get files change mimeType if you want video), PICK for GALLERY (should change mimetype if u want video) CHOOSER for GALLERY (if someone have other image browsers)

You can also check in logcat what queries you have to add (should contains "BLOCKED" or "no permission". Error is because ImagePickerModule when you don't have permission in Intent with resolveActivity returns null (you can comment it to check better errors in startActivityForResult)

Add <query> in AndroidMenifest.xml

<manifest>
.....
.....
<queries>
    <!-- Browser -->
    <intent>
        <action android:name="android.intent.action.VIEW" />
        <data android:scheme="http" />
    </intent>
    <!-- Camera -->
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
    <!-- Gallery -->
    <intent>
        <action android:name="android.intent.action.GET_CONTENT" />
        <data android:mimeType="image/*" />
    </intent>
    <intent>
        <action android:name="android.intent.action.PICK" />
        <data android:mimeType="image/*" />
    </intent>
    <intent>
        <action android:name="android.intent.action.CHOOSER" />
     </intent>

     <!--For external application-->
     <package android:name="com.whatsapp" />
</queries>
.....
.....
</manifest>
ND1010_
  • 3,743
  • 24
  • 41
  • 1
    I had the same issue. The solution you provided helped me a bit, but still, I don't get all the Apps that are usually available for sharing (like WhatsApp, Facebook etc.) What is missing? – Mehmed Mert Sep 03 '21 at 15:22
  • 3
    I just realized that the newly added permission `android.permission.QUERY_ALL_PACKAGES` was missing in the manifest. So, adding additionally this line solved my problem: `` – Mehmed Mert Sep 03 '21 at 15:34
  • @MehmedMert you can also add the particular package under the `` to resolve the issue for example for Whatsapp you can add `` Please check my updated answer for batter idea – ND1010_ Sep 04 '21 at 09:16
  • perfect answer <3 – ashif-ismail Apr 17 '22 at 11:39
1
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
        tools:ignore="QueryAllPackagesPermission" />

Don't forget to paste this line of code in manifest