Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

W/CameraBase﹕ An error occurred while connecting to camera: 0 on camera.open() call

I'm writing a camera app and whenever I call camera.open() the app crashes and then I get this error:

W/CameraBase﹕ An error occurred while connecting to camera: 0

Here is how I'm opening the camera:

public void getCameraInstance(){
    mCamera = null;

    try 
    {
        mCamera = Camera.open(); // attempt to get a Camera instance
    }

    catch (Exception e)
    {
        // Camera is not available (in use or does not exist)

    }
}

UPDATE:

If you are reading this please note that this is for the original camera API and no longer applies the the latest version of the camera api (camera2).

You should use the camera2 api from this point onwards as it has greater functionality and also has a better image processing pipeline.

NOTE ONLY VALID UP TO excluding API 21 (Lolipop) i.e. does not apply for Lolipop and above.

like image 503
Cjen1 Avatar asked Oct 08 '14 16:10

Cjen1


3 Answers

You manualy uploaded your application to phone. That is why camera permission is not approved. You have to open settings->applications (or something like that) and manualy approve this permission.

like image 178
Yuri Smirnoff Avatar answered Nov 16 '22 22:11

Yuri Smirnoff


In Android 6, make sure you request permission for the camera. Camera access is considered one of the 'dangerous permissions'.

like image 30
Timmmm Avatar answered Nov 16 '22 23:11

Timmmm


To use the following method

android.hardware.Camera.open(int cameraId)

You should pass cameraId, If you want the front camera Id you can use the following method

private int findFrontFacingCamera() {

    // Search for the front facing camera
    int numberOfCameras = Camera.getNumberOfCameras();
    for (int i = 0; i < numberOfCameras; i++) {
        CameraInfo info = new CameraInfo();
        Camera.getCameraInfo(i, info);
        if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
            cameraId = i;
            cameraFront = true;
            break;
        }
    }
    return cameraId;
}
  1. If the same camera is opened by other applications, this will throw a RuntimeException.

  2. You must call release() when you are done using the camera, otherwise it will remain locked and be unavailable to other applications.

  3. Your application should only have one Camera object active at a time for a particular hardware camera.

like image 11
madhu131313 Avatar answered Nov 16 '22 23:11

madhu131313