0

I'm writing the camera application. I've started with using android.hardware.Camera and corresponded classes. But from time to time my application failed to connect to the camera. It depends on device model and Android version. But if application works on some device fine, after a day or two it start to failing. One more thing that I noticed - if my app fails to connect then build-in camera app fails to connect too.

I read almost all post related to Camera API and related exceptions on stackoverflow and decided to replace my code with CommonsGuy's library https://github.com/commonsguy/cwac-cam2. And, unfortunately, fails are still exists.

I discovered that after fail I can use Camera API again only after device reboot.

So I want to ask is it possible to do something programmatically for making possible to use camera again without rebooting device? Maybe there is a way to kill Camera Service or upload native Camera code?

Example of fail (Nexus 4, Android 5.1):

Fatal Exception: java.lang.RuntimeException: Fail to connect to camera service
   at android.hardware.Camera.<init>(Camera.java:497)
   at android.hardware.Camera.open(Camera.java:342)
   at com.commonsware.cwac.cam2.ClassicCameraEngine$1.run(ClassicCameraEngine.java:78)
   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
   at java.lang.Thread.run(Thread.java:818)
Ilya Tretyakov
  • 6,848
  • 3
  • 28
  • 45
  • Release the camera in different cases like changing front to back or vise-versa, onDestroy() method etc. Do some code: `mCamera.stopPreview(); mCamera.release();` – Dhruv May 09 '16 at 05:53
  • i also face this issue in my phone because of broken front camera connector. where the solution was to reboot the device while pressing camera connector part – Nilabja May 09 '16 at 07:16
  • If this happens with your app more often than with the built-in Camera app, then your code has some bug, not releasing the camera when necessary. There may be some tricky thread collisions when the app stops, and it is easy to lose track of the camera. – Alex Cohn May 09 '16 at 11:13
  • 1
    On a rooted device, it's trivial to kill the MediaServer. On non-rooted device, there is no known vulnerability that could force its restart, at least nothing that will work across all ROMs and models. – Alex Cohn May 09 '16 at 11:16

2 Answers2

2

So I want to ask is it possible to do something programmatically for making possible to use camera again without rebooting device?

No, sorry.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

check this ref: http://developer.android.com/training/camera/cameradirect.html try this code:

try
{
    releaseCameraAndPreview();
    if (camId == 0) {
        mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_FRONT);
    } else {
        mCamera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK);
    }
}

catch(Exception e)
{
    Log.e(getString(R.string.app_name), "failed to open Camera");
    e.printStackTrace();
}

Then add this function somewhere:

private void releaseCameraAndPreview() {
    myCameraPreview.setCamera(null);
    if (mCamera != null) {
        mCamera.release();
        mCamera = null;
    }
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
Damini Mehra
  • 3,257
  • 3
  • 13
  • 24