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.
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.
In Android 6, make sure you request permission for the camera. Camera access is considered one of the 'dangerous permissions'.
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;
}
If the same camera is opened by other applications, this will throw a RuntimeException.
You must call release() when you are done using the camera, otherwise it will remain locked and be unavailable to other applications.
Your application should only have one Camera object active at a time for a particular hardware camera.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With