Release Android Camera without restart?

16,045

Solution 1

Camera resource management is done by the android Camera Service (libcameraservice.so). The process that runs Camera Service is mediaserver. If you restart the Media Server, you'll effectively release all camera locks.

You must have adb root access to do that.

Run adb shell; su to get root command prompt.

root@m0:/ # ps|grep media
media     1901  1     129268 6168  ffffffff 4026d470 S /system/bin/mediaserver
media_rw  1909  1     3064   1380  ffffffff 4015f1ac S /system/bin/sdcard
u0_a19    22821 1899  484316 21588 ffffffff 4016d574 S android.process.media
root@m0:/ # kill 1901
kill 1901

Media server should be started automatically now.

Android 7.0

The lastest version of Android has the mediaserver binary split into multiple dedicated binaries to help with security. Read more in the official documentation.

So instead of the mediaserver you should look for the cameraserver. Not tested though.

Solution 2

I'm not sure if there's a way to reset the camera through terminal or DDMS, but I've had a lot of success by putting my call to Camera.release() in my Activity's onPause() method, so the camera gets released whenever the App goes into the background. Similarly, my Camera initialization all happens in onResume(), so the camera will be opened on first run, then released and re-initialized again as your app moves to and from the background. This technique has completely eliminated the problem that you're describing for me.

If the problem persists for you, also consider wrapping the code that's likely to crash in a try/catch, where you catch any Exception and make the call to release the camera in that catch block. I usually use this technique for errors that can be smoothed over without shutting down the app, however I believe it will work in all cases.

Solution 3

I find a method to solve this problem. Setting----->Applications------>Manage Applications----->camera--->clear data ,then the problem is solved

Solution 4

Here's a solution that installs a UncaughtExceptionHandler: https://stackoverflow.com/a/17336974/755804

It releases the camera when the app dies.

Share:
16,045

Related videos on Youtube

MattDavis
Author by

MattDavis

Android Engineer at Beme in New York City.

Updated on June 04, 2022

Comments

  • MattDavis
    MattDavis almost 2 years

    I'm currently trying to get the camera working in my app and I'm having issues with the whole camera.release() thing; I'm doing pretty complicated stuff and there's no clear cut way to simply call camera.release() so I have to keep trying new ways, but every time I get it wrong my app crashes (as expected), leaving the camera not released. So, in order to be able to test my app again, I need to restart my phone in order to refresh the camera.

    Is there any way to release the camera through the terminal or DDMS, or is the only way to release it by restarting my phone? It's getting pretty old, especially when a tiny mistake leads to wasted time waiting for my phone to restart.

    Thanks!

    • olafure
      olafure almost 11 years
      Did you ever find a solution to this ?
  • George Profenza
    George Profenza almost 11 years
    It's weird, after I override onPause() to release the cam and onPause to open the cam, I get "Fail to connect to camera service" every single time I launch the app after it's constructed. Currently I only destroy the camera in the surfaceDestroyed callback and it works well most of the time. Sometimes I do get the "fail to connect to camera service error" but it's hard to work out when/what causes that. Any hints on what I can look for ? Silently failing isn't an option, I do need the phone to take photos reliably
  • MattDavis
    MattDavis almost 11 years
    How are you initializing the camera? In my code, I make the call to camera.open, then pass my Camera object to the surface view to call setPreviewDisplay and startPreview. I don't use surfaceDestroyed for anything, I manage the camera entirely in the Activity Lifecycle and just pass the reference to the view to start the preview. This is how it works for me.
  • George Profenza
    George Profenza almost 11 years
    I see...I was missing setPreviewDisplay. I've just discovered the CameraPreview sample from the SDK which has this implemented like you mentioned. Thanks for the hint (+1)
  • George Profenza
    George Profenza almost 11 years
    this looks handy(+1), although I'm guessing it's cleaner to make the app tight/handle all possible errors elegantly.
  • blganesh101
    blganesh101 almost 11 years
    Yup! very much true. ideally we should not be handling a global handler.