Android - Fail to connect to camera

前端 未结 9 917
日久生厌
日久生厌 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 20:48

    I also get this type of issue on a HTC device. To solve add this code:

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        if (camera!=null)
        {
            camera.stopPreview();
            camera.release();
            camera=null;
        }
    }
    

    And yet you cannot start camera then restart device.

    0 讨论(0)
  • 2020-12-30 20:52

    It happens if your activity does not close the camera properly in surfaceDestroyed or onConfigurationChanged etc...

    Don't forget to do this everytime you go out of your activity:

            if (camera!=null){
                    camera.stopPreview();
                    camera.release();
                    camera=null;
            }
    
    0 讨论(0)
  • 2020-12-30 20:54

    As others mention, you have to call release() on your camera object when you're finished.

    I wasn't doing this initially, so I changed my code but it still gave me the same error. I was deploying directly to a physical handset and had to restart the phone before it worked

    0 讨论(0)
  • 2020-12-30 20:57

    Also, if you are using the emulator, make sure you have selected to Emulate the Front Camera and/or the Back Camera.

    Android Virtual Device Manager->Select Device->Edit->Front Camera->Emulated

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

    Another reason of this error is when you try to open camera but some other application or even your application is already using camera.

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

    Second @matt-burns however you might want to check that you're only trying to get the camera once. I had forgotten to comment out a line and was trying to launch two activities that would both try to obtain the camera.

    0 讨论(0)
提交回复
热议问题