I need some help. We are created an app that uses the front facing camera only. However on some devices we are facing a problem. So on certain devices the Camera.open() method throws an exception:
Failed to connect to camera service.
The log differs from device to device, but is one of 2 messages:
Camera W 21325 Camera server died!
or
CameraBase W 18851 An error occurred while connecting to camera: 1
On other devices it works fine. Here's the code i'm using to access the camera-
public Camera getFrontFacingCamera() {
Camera object = null;
try {
object = Camera.open(findFrontFacingCamera());
} catch (Exception e) {
Mint.logException(e);
}
return object;
}
private static int findFrontFacingCamera() {
int cameraId = -1;
@SuppressWarnings("deprecation")
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;
break;
}
}
return cameraId;
}
private void onCreate() {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
cameraObject = getFrontFacingCamera();
showCamera = new ShowCamera(this, cameraObject);
preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
@Override
protected void onPause() {
super.onPause();
if (cameraObject != null) {
cameraObject.release();
cameraObject = null;
preview.removeView(showCamera);
showCamera = null;
}
ShowCamera.java
public class ShowCamera extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder holdMe;
private Camera mCamera;
Context context;
public ShowCamera(Context context,Camera camera) {
super(context);
this.context=context;
mCamera = camera;
holdMe = getHolder();
holdMe.addCallback(this);
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
} catch (IOException e) {
}
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
this.getHolder().removeCallback(this);
mCamera.release();
}
}
And the manifest:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.front" android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
Does anyone know how to correct this issue?