0

I try to open the front facing camera but Camera.open( camId ) returns null and throws the exception java.lang.RuntimeException: Fail to connect to camera service.

I've looked around at similar problems and tried what I've found in those threads but it doesn't appear to be working for my program. I think it might have something to do with the fact that I try to run the camera as a service where I don't want any preview.

This is my start recording method:

Log.v(TAG, "Starting camera");
Intent intent = new Intent(getActivity(), RecorderService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startService(intent);

and this is my stop method:

Log.v(TAG, "Stopping camera");
getActivity().stopService(new Intent(getActivity(), RecorderService.class));

In the service's onStartCommand method i set all the settings according to Android documentation. I have made this exact code work in an app but when I try to "detach" it by implementing it in my Recorder.java class, I can't really seem to work around the case that I'm not opening it from an activity...

What I've tried:

  1. The permissions in Android Manifest (camera, record_audio, record_video) and also uses-feature (camera and camera.front)

  2. The camera is properly closed (stop(), reset(), stopPreview(), release())

  3. Implement the service in a separate application. This works but as I want it to be "detached" from an activity I can't go into activities and start the camera from there. I'd prefer it to start from my Recorder.java which is supposed to be easily implemented into your application for user testing...

I suspect it might be the SurfaceHolder holding me back. Is it possible to make the camera record without dealing with SurfaceViews and SurfaceHolders in the activity?

Thanks for any help

More code (this is where the exception comes from):

private static Camera openFrontFacingCamera() {
    int cameraCount = 0;
    Camera cam = null;

    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();

    Log.v(TAG, "Number of cameras: " + cameraCount);

    for ( int camIdx = 0; camIdx < cameraCount; camIdx++ ) {

        Camera.getCameraInfo( camIdx, cameraInfo );

        if ( cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT ) {

            try {

                cam = Camera.open( camIdx );

            } catch (Exception e){

                // Camera is not available (in use or does not exist)
                Log.d(TAG, "Camera did not open");
                Log.d(TAG, e.toString());
                e.printStackTrace();


            }
        }
    }

    return cam;
}
mnordber
  • 1,143
  • 2
  • 13
  • 18

1 Answers1

0

In your log

Log.d(TAG, "Camera did not open");

change it to

Log.d(TAG, "Camera did not open, camId= "+camIdx);

I think you will come to know the answer. Does it throw exception for camIdx = 0 ?

If no, then I think your problem is that you are not stopping camera service before starting for camIdx= 1

Kaustuv
  • 811
  • 5
  • 9
  • The exception is thrown from camIdx = 1. I have read about people having problems where the camera only worked the first time they tried because they forgot to stop and release the camera. However, my camera doesn't work at all. I've tried on a rebooted phone to make sure no other applications might have used the camera before. The openFrontFacingCamera comes from [this SO thread](http://stackoverflow.com/questions/2779002/how-to-open-front-camera-on-android-platform) so I don't think there's anything wrong in it? Thanks for your answer! – mnordber May 10 '14 at 10:34