1

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?

  • are you getting this error each time you are trying to open the camera on the problematic device? – royB Dec 29 '14 at 15:45
  • No, it's actually pretty random. It is recurring on problematic devices for some time, but then it's doesn't. I think it has to do with some app(maybe mine) not releasing the camera correctly, but if it's not my app, i can't really release a camera in another app it seems – Dmitry Chesnokov Dec 29 '14 at 16:09
  • can you add the code where you release the camera? – royB Dec 29 '14 at 16:22
  • Yeah, added it in the edit – Dmitry Chesnokov Dec 30 '14 at 10:42
  • why are you releasing the camera only if the activity is in pause? what about releasing it when the activity finishes? (this is controlled by the isFinishing() param – royB Dec 30 '14 at 10:55
  • Hm, i must have left it there even though i deleted the onStop method. A dumb mistake, thanks for pointing that out. Didn't help though, still get the same error. – Dmitry Chesnokov Dec 30 '14 at 11:28
  • are you using `SurfaceHolder`? if so can you please add the `surfaceDestroyed` code? – royB Dec 30 '14 at 11:34
  • look at here http://stackoverflow.com/questions/6098729/android-front-camera you need to check if front camera is available or not – Key_coder Dec 30 '14 at 11:52
  • The problematic devices have front cameras and i've debugged findFrontFacingCamera() loop and it finds them there, it just cannot open them for some reason – Dmitry Chesnokov Dec 30 '14 at 14:54

0 Answers0