LittleMonsterr
2017-03-27 05:29
采纳率: 100%
浏览 20.2k

调用相机camera.open失败Fail to connect to camera service

权限有了

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />
    <!-- 调用硬件相机权限 -->
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
         @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (!hasSurface) {
            hasSurface = true;
            initCamera(holder);
        }

    }

         @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        hasSurface = false;
        if (camera != null) {
            if (CameraManager.get().isPreviewing()) {
                if (!CameraManager.get().isUseOneShotPreviewCallback()) {
                    camera.setPreviewCallback(null);
                }
                camera.stopPreview();
                camera.release();
                camera = null;
                CameraManager.get().getPreviewCallback().setHandler(null, 0);
                CameraManager.get().getAutoFocusCallback().setHandler(null, 0);
                CameraManager.get().setPreviewing(false);
            }
        }
    }

         private void initCamera(SurfaceHolder surfaceHolder) {
        try {
            //6.0+调用相机权限
            if (ContextCompat.checkSelfPermission(getContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions((Activity) getContext(),
                        new String[]{Manifest.permission.CAMERA},
                        TAKE_PHOTO_REQUEST_CODE);
            }else {
                camera = Camera.open();
                parameter = camera.getParameters();
                CameraManager.get().openDriver(surfaceHolder);
                camera = CameraManager.get().getCamera();
                camera.startPreview();
            }

        } catch (IOException | RuntimeException e) {
            L.e(e.getMessage());
            return;
        }
        if (handler == null) {
            handler = new CaptureActivityHandler(this, decodeFormats, characterSet, viewfinderView);
        }
    }

请问少了什么,为什么会失败?
在6.0上测试没问题,换了红米2A 4.4的系统就不行了。
求解
谢谢~

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • LittleMonsterr 2017-03-28 02:55
    已采纳

    已解决,参考 http://blog.csdn.net/jdsjlzx/article/details/43098179


    @Override
    public void onPause() {
    super.onPause();
    if (handler != null) {
    handler.quitSynchronously();
    handler = null;
    }
    // 关闭摄像头
    CameraManager.get().closeDriver();
    //添加以下代码,问题解决。
    ** if (!hasSurface) {
    SurfaceView surfaceView = (SurfaceView) view.findViewById(R.id.preview_view);
    SurfaceHolder surfaceHolder = surfaceView.getHolder();
    surfaceHolder.removeCallback(this);
    }**
    }

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • shen_wei 2017-03-27 06:47
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题