Android - Fail to connect to camera

前端 未结 9 918
日久生厌
日久生厌 2020-12-30 20:29

I\'m using the Android APIDemo sample code.

When I run the CameraPreview example, at first it was giving me an error.

I traced that one down and the sample

相关标签:
9条回答
  • 2020-12-30 21:02

    Make sure your <uses-permission> elements are in the proper positions in your AndroidManifest.xml file.

    0 讨论(0)
  • 2020-12-30 21:10

    I also received this error when I was testing and stopped execution before reaching the point in code when the:

    if (camera!=null){
        camera.stopPreview();
        camera.release();
        camera=null;
    }
    

    was called. This then blocked the camera because it hadn't een released properly. My solution was to turn the camera off and back on again. You can confirm this is the case by trying to use the inbuilt Camera app in your phone. It won't work either because it is still busy.

    0 讨论(0)
  • 2020-12-30 21:11

    Be sure to properly release all the aquired camera resources:

        @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mCam != null) {
            mCam.stopPreview();
            mCam.setPreviewCallback(null);
            mCam.release();
            mCam = null;
        }
    }
    
        @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mCam == null) {
            mCam = Camera.open();
            try {
                mCam.setPreviewDisplay(holder);
    
                // TODO test how much setPreviewCallbackWithBuffer is faster
                mCam.setPreviewCallback(this);
            } catch (IOException e) {
                mCam.release();
                mCam = null;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题