Android 11使用Camera捕获图像

问题描述

文档-https://developer.android.com/training/camera/photobasics

我已按照所有必需的步骤使用相机捕获图像。

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // its always null
        startActivityForResult(takePictureIntent,REQUEST_IMAGE_CAPTURE);
    }
}

takePictureIntent.resolveActivity(getPackageManager())-此行始终返回null。并且如果我跳过此检查,则相机打开但应用程序崩溃。

解决方法

将它们添加到清单部分的AndroidManifest.xml中:

<queries>
  <intent>
    <action android:name="android.media.action.IMAGE_CAPTURE" />
  </intent>
</queries>
,

Jaakko的答案是正确的,这是一个简短的解释:

代码:

<manifest package="your.package.name">
<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>
</manifest>

仅适用于默认相机应用程序。如果您的应用使用的是第三方摄像机,则可以在此处找到一些信息:https://commonsware.com/blog/2020/08/16/action-image-capture-android-r.html

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...