设备:模拟器像素 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 asqueryIntentActivities()
, 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/