Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 11 (R) return empty list when querying intent for ACTION_IMAGE_CAPTURE

Device: Emulator pixel 3a - Android 11

Code:

    final List<Intent> cameraIntents = new ArrayList<Intent>();     final Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);     final List<ResolveInfo> listCam =      context.getPackageManager().queryIntentActivities(captureIntent, 0); 

When using:

targetSdkVersion 30 compileSdkVersion 30 

listCam size is 0

and when changing to:

compileSdkVersion 29 

listCam size is 1 - as it should be.

Using the following code:

    val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)     baseActivity.startActivity(captureIntent) 

Works fine and shows the camera app.

Any idea why queryIntentActivities is not returning the camera intent?

Thanks!

like image 869
Udi Oshi Avatar asked Aug 04 '20 12:08

Udi Oshi


Video Answer


1 Answers

Android 11 changes how apps can query and interact with other apps.

From the docs:

The PackageManager methods that return results about other apps, such as queryIntentActivities(), are filtered based on the calling app's <queries> declaration.

So you need to declare <queries> in your AndroidManifest.xml:

<manifest package="com.example">     <queries>         <intent>             <action android:name="android.media.action.IMAGE_CAPTURE" />         </intent>     </queries>     ... </manifest> 
like image 83
Saurabh Thorat Avatar answered Sep 29 '22 15:09

Saurabh Thorat