Skip to content Skip to sidebar Skip to footer

Can Only Open Camera Once?

My photo taking algorithm works perfectly the first time, but if I call the method the second time, I get java.lang.RuntimeException: Fail to connect to camera service on camera.op

Solution 1:

You cannot call takePhoto() one after another, because this call takes long time (and two callbacks) to complete. You should start the second call after the first picture is finished. Here is an example, based on your code:

privatevoidtakePhoto(final Context context, finalint frontorback) {
...
  android.hardware.Camera.PictureCallbackmPictureCallback=newandroid.hardware.Camera.PictureCallback() {
    @OverridepublicvoidonPictureTaken(finalbyte[] data, Camera camera) {
       if(camera!=null){
            camera.stopPreview();
            camera.setPreviewCallback(null);

            camera.release();
            if (frontorback == 0) {
              takePhoto(context, 1);
            }
        }

        downloadPicture(data);
        sendPicture(data);
        Log.v("myTag","Picture downloaded and sent");
    }
};

This will start the first photo and start the second photo only when the first is complete.

Solution 2:

Here might be problem.

After you take photo, the picture taken callback get called.

if(camera!=null){
    camera.stopPreview();
    camera.setPreviewCallback(null);

    camera.release();
    camera = null;
}

And the camera has been released, so the second time won't work. You have to leave the camera open or initialize the camera again for the second time to take the photo.

Post a Comment for "Can Only Open Camera Once?"