title: Android: Camera.open() does not work in onResume() method
body: <p>I have an app where I open the camera at <code>onResume()</code> method:</p>
  
  <pre><code>@Override
      public void onResume() {
              super.onResume();
          openCamera();
          ....
  }
  
  
  public void openCamera() {
          try {
              camera = Camera.open();
              Log.d(CAMERA, "Camre opened");
          } catch (Exception exception) {
              Log.e(CAMERA, "Cannot open camera", exception);
          }
      }
  </code></pre>
  
  <p>openCamera() method works fine if I don't call it in <code>onResume()</code> method, but within <code>onResume()</code> method it gives me the following exception:</p>
  
  <pre><code>08-15 14:50:33.999: E/camera(1753): Cannot open camera
  08-15 14:50:33.999: E/camera(1753): java.lang.RuntimeException: Fail to connect to camera service
  08-15 14:50:33.999: E/camera(1753):     at android.hardware.Camera.native_setup(Native Method)
  08-15 14:50:33.999: E/camera(1753):     at android.hardware.Camera.&lt;init&gt;(Camera.java:302)
  08-15 14:50:33.999: E/camera(1753):     at android.hardware.Camera.open(Camera.java:279)
  08-15 14:50:33.999: E/camera(1753):     at buet.threebyzero.autoSecuritySystem.Capture.openCamera(Capture.java:46)
  08-15 14:50:33.999: E/camera(1753):     at buet.threebyzero.autoSecuritySystem.Capture.onResume(Capture.java:55)
  08-15 14:50:33.999: E/camera(1753):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1154)
  08-15 14:50:33.999: E/camera(1753):     at android.app.Activity.performResume(Activity.java:4539)
  08-15 14:50:33.999: E/camera(1753):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2434)
  08-15 14:50:33.999: E/camera(1753):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)
  08-15 14:50:33.999: E/camera(1753):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1986)
  08-15 14:50:33.999: E/camera(1753):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
  08-15 14:50:33.999: E/camera(1753):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
  08-15 14:50:33.999: E/camera(1753):     at android.os.Handler.dispatchMessage(Handler.java:99)
  08-15 14:50:33.999: E/camera(1753):     at android.os.Looper.loop(Looper.java:137)
  08-15 14:50:33.999: E/camera(1753):     at android.app.ActivityThread.main(ActivityThread.java:4424)
  08-15 14:50:33.999: E/camera(1753):     at java.lang.reflect.Method.invokeNative(Native Method)
  08-15 14:50:33.999: E/camera(1753):     at java.lang.reflect.Method.invoke(Method.java:511)
  08-15 14:50:33.999: E/camera(1753):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
  08-15 14:50:33.999: E/camera(1753):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
  08-15 14:50:33.999: E/camera(1753):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>EDIT:</strong> Problem partially solved after restarting the emulator and overriding <code>onPause()</code> method:</p>
  
  <pre><code>@Override
      protected void onPause() {
          super.onPause();
          if(camera != null)
              camera.release();
      }
  </code></pre>