Android 11 (R) 在查询 ACTION_IMAGE_CAPTURE 的 Intent 时返回空列表

标签 android android-intent android-camera android-camera-intent android-intent-chooser

设备:模拟器像素 3a - Android 11
代码:

    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);
使用时:
targetSdkVersion 30
compileSdkVersion 30
listCam 大小为 0
当更改为:
compileSdkVersion 29
listCam 大小为 1 - 应该是。
使用以下代码:
    val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
    baseActivity.startActivity(captureIntent)
工作正常并显示相机应用程序。
知道为什么 queryIntentActivities 没有返回相机 Intent 吗?
谢谢!

最佳答案

Android 11 改变了应用查询和与其他应用交互的方式。
来自 docs :

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


所以你需要声明<queries>在您的 AndroidManifest.xml :
<manifest package="com.example">
    <queries>
        <intent>
            <action android:name="android.media.action.IMAGE_CAPTURE" />
        </intent>
    </queries>
    ...
</manifest>

关于Android 11 (R) 在查询 ACTION_IMAGE_CAPTURE 的 Intent 时返回空列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63246442/

相关文章:

android - 应用程序关闭时从 BLE 设备向手机推送通知或 Intent

Android 设备在设置 FPS 时崩溃,而它在 getSupportedPreviewFpsRange 范围内

java - 在android中创建全局函数

android - 无法正确获取 "android.intent.action.SEND" Intent 的路径

android - Intent 的标志和 PendingIntent.getBroadcast

android - 如何在android中组合覆盖位图和捕获的图像?

java - Galaxy S4 Android 自定义相机问题

android - 当 listview 滚动时 edittext 设置默认值

java - 从android应用程序发送数据(Client_id=1,Staff_id=2)到tomcat服务器

android - 如何停止 fcm 后台自动生成的通知?