title: Failed to connect to camera service in Android
body: <p>Got an exception "failed to connect to camera service" when navigating from another activity to camera activity.The issue occurs at Camera.open().I have searched a lot and tried the solutions available.But the issue is not solved.Please provide some help.Thanks in advance.</p>
  
  <pre><code> @Override
      public void surfaceChanged(final SurfaceHolder holder, int arg1,
              int arg2, int arg3) {
  
  
          cameraHandler = new Handler() {
              /*
               * (non-Javadoc)
               * 
               * @see android.os.Handler#handleMessage(android.os.Message)
               */
              @Override
              public void handleMessage(Message msg) {
                  switch (msg.what) {
                  case LOADCAMERA:
                      if (null != mProgress) {
                          if (mProgress.isShowing()) {
                              mProgress.dismiss();
                          }
                      }
                      try {
                          try {
  
                              mCam = Camera.open(); // attempt to get a Camera
                                                      // instance
                          } catch (Exception e) {
                              Log.e("Exception", e.getMessage());
                          }
                          mCam.setPreviewDisplay(holder);
                          mCameraParameters = mCam.getParameters();
                          if (isFlashLightOn) {
                              mCameraParameters
                                      .setFlashMode(Parameters.FLASH_MODE_TORCH);
                          } else {
                              mCameraParameters
                                      .setFlashMode(Parameters.FLASH_MODE_OFF);
                          }
                          mCameraParameters.setPreviewSize(mVideoWidth,
                                  mVideoHeight);
                          mCam.setParameters(mCameraParameters);
                          mCam.startPreview();
                          mCam.unlock();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
  
                  }
                  super.handleMessage(msg);
              }
          };
          Message msg = new Message();
          msg.what = LOADCAMERA;
          cameraHandler.sendMessageDelayed(msg, 700);
  
      }
  
      @Override
      public void surfaceCreated(SurfaceHolder holder) {
  
      }
  
      @Override
      public void surfaceDestroyed(SurfaceHolder arg0) {
  
  
          if (mCam != null) {
              mCam.stopPreview();
              mCam.setPreviewCallback(null);
              mCam.release();
              mCam = null;
          }
  
      }
  }
  </code></pre>
  
  <p>Log cat is added</p>
  
  <pre><code>W/CameraService(   78): CameraService::connect X (pid 623) rejected (camera 0 is still      busy).
  E/Exception(  623): Fail to connect to camera service
  D/AndroidRuntime(  623): Shutting down VM
  W/dalvikvm(  623): threadid=1: thread exiting with uncaught exception(group=0x40015560)
  E/AndroidRuntime(  623): FATAL EXCEPTION: main
  E/AndroidRuntime(  623): java.lang.NullPointerException
  E/AndroidRuntime(  623):    at   com.gui.MainActivity$CameraPreview$1.handleMessage(MainActivity.java:2041)
  E/AndroidRuntime(  623):    at android.os.Handler.dispatchMessage(Handler.java:99)
  E/AndroidRuntime(  623):    at android.os.Looper.loop(Looper.java:130)
  E/AndroidRuntime(  623):    at  android.app.ActivityThread.main(ActivityThread.java:3683)
  E/AndroidRuntime(  623):    at java.lang.reflect.Method.invokeNative(Native Method)
  E/AndroidRuntime(  623):    at java.lang.reflect.Method.invoke(Method.java:507)
  E/AndroidRuntime(  623):    at    com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:845)
  E/AndroidRuntime(  623):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:603)
  E/AndroidRuntime(  623):    at dalvik.system.NativeStart.main(Native Method)
  W/ActivityManager(  106):   Force finishing activity com.gui/.MainActivity
  W/ActivityManager(  106):   Force finishing activity com.gui/.MainActivity
  E/v4l2_utils(   78): Error = Try again from dqbuf
  E/SEC_Overlay(   78): Failed to DQ/-1
  E/CameraHardwareSec(   78): ERR(int    android::CameraHardwareSec::previewThread()):overlay dequeueBuffer fail
  I/CameraHardwareSec(   78): int android::CameraHardwareSec::previewThreadWrapper():  calling mSecCamera-&gt;stopPreview() and waiting
  I/CameraHardwareSec(   78): int android::CameraHardwareSec::previewThreadWrapper(): return from wait
  I/CameraHardwareSec(   78): int android::CameraHardwareSec::previewThreadWrapper(): exiting
  W/SecCamera(   78): int android::SecCamera::stopPreview(): doing nothing because m_flag_camera_start is zero
  I/CameraHardwareSec(   78): virtual void android::CameraHardwareSec::release(): calling mPreviewHeap.dispose()
  W/SecCamera(   78): int android::SecCamera::stopRecord(): doing nothing because     m_flag_record_start is zero
  I/SecCamera(   78): DeinitCamera: m_cam_fd(15)
  I/SecCamera(   78): DeinitCamera: m_cam_fd2(21)
  </code></pre>