首页 > 解决方案 > Camera.open() 获取“访问权限已被限制”

问题描述

然而,当尝试 Camera.open(i) 检查相机属性时,我得到

I/CameraService:Camera 0:“包裹”的访问受到限制

显然,这只有在应用程序的包或 uId 已被明确限制启动 AppOpsManager#OP_CAMERA 时才会发生 [1]。但是为什么会这样呢?后AppOpsManager#checkOpAppOpsManager#MODE_ALLOWED

示例代码

for (int i = 0; i < numCameras; ++i) {
    List<Size> supportedSizes = null;
    List<int[]> supportedFpsRanges = null;
    Camera camera = null;
    try {
        camera = Camera.open(i);
        Parameters parameters = camera.getParameters();
        supportedSizes = parameters.getSupportedPreviewSizes();
        supportedFpsRanges = getFpsRangesRobust(parameters);
    } catch (Exception e) {
        ...
    } finally {
        if (camera != null) {
            camera.release();
        }
    }

[1] https://github.com/aosp-mirror/platform_frameworks_base/blob/oreo-mr1-release/services/core/java/com/android/server/AppOpsService.java#L2588

标签: androidandroid-cameraandroid-7.1-nougat

解决方案


显然这是一个设备问题。诺基亚 6 / TA-1000,安卓 7.1.1

情景

  • 尝试使用相机
  • 应用程序请求许可,点击拒绝
  • 尝试使用相机
  • 应用程序请求许可,点击允许
  • 在 logcat 中发布错误,相机无法工作

可通过任何其他应用程序重现,包括默认相机应用程序。


推荐阅读