前言
android 11 调用系统摄像头有了新的策略,需兼容才可行
1、在Android11之前调用系统摄像头代码:
Intent captureIntent = new Intent("android.media.action.IMAGE_CAPTURE");
if (captureIntent.resolveActivity(this.getPackageManager()) != null) {}
2、在Android11之后,captureIntent.resolveActivity(this.getPackageManager())方法将返回null,导致调用相机失败。
Starting in Android 11, only pre-installed system camera apps can respond to the following intent actions:
android.media.action.VIDEO_CAPTURE
android.media.action.IMAGE_CAPTURE
android.media.action.IMAGE_CAPTURE_SECURE
If more than one pre-installed system camera app is available, the system presents a dialog for the user to select an app. If you want your app to use a specific third-party camera app to capture images or videos on its behalf, you can make these intents explicit by setting a package name or component for the intent.
优化方案
在manifest中配置queries
<manifest package="your.package.name">
<queries>
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
</queries>
</manifest>