18

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!

MattDavis
  • 5,158
  • 2
  • 23
  • 35

5 Answers5

18

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.

Vanuan
  • 31,770
  • 10
  • 98
  • 102
  • 3
    Great answer! I have a Samsung Galaxy S3 and ever since switching to Cyanogenmod the camera is sometimes giving the "can't connect to camera error". Your solution allowed me to get the camera working again without restarting the whole phone. I decided to create an app to do that automatically: https://play.google.com/store/apps/details?id=com.exlyo.camerarestarter I hope that helps other people :-) – androidseb Oct 18 '15 at 02:17
  • any chance of posting the source of your app? How do you find the process id of the mediaserver process? – yeahman Feb 13 '16 at 13:32
  • I think you can do it parsing ps output. – Vanuan Feb 13 '16 at 22:52
  • 2
    The full source code of the "Camera restarter" app is available here: https://github.com/androidseb/camerarestarter – androidseb Apr 08 '16 at 14:28
  • It appears this solution doesn't work anymore on CM 14, anybody has a solution for CM 14? – androidseb Dec 19 '16 at 18:03
  • @androidseb it looks like Android 7.0 has got a new mediaserver. Android 7.0 breaks up the monolithic mediaserver process into multiple processes with permissions and capabilities restricted to only those required by each process. Read here: https://source.android.com/devices/media/framework-hardening.html – Vanuan Dec 21 '16 at 19:45
  • @androidseb BTW, I saw comments on playstore about this method not working. I think it should be clarified in the description that it would only work for an open source version of Android. E.g. in Samsung firmware they've completely ditched this mediaserver system because of custom camera capabilities not being provided by AOSP back then. – Vanuan Dec 21 '16 at 19:52
  • @Vanuan Thank you for your replies. With the help of a user I was able to find an improvement to your technique and confirm it works. The process listed with the user "camera" and with the file "/system/bin/cameraserver" needs to be killed as well starting with CM 14 for the "restart" to work. I updated the source code of my app accordingly and it's now fixed with version 4.0.0 – androidseb Dec 22 '16 at 23:12
4

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.

MattDavis
  • 5,158
  • 2
  • 23
  • 35
  • 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 – George Profenza Jun 25 '13 at 12:15
  • 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. – MattDavis Jun 25 '13 at 13:56
  • 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 Jun 26 '13 at 17:07
  • No problem. To supplement the SDK sample, you may want to check out http://developer.android.com/guide/topics/media/camera.html and http://developer.android.com/training/camera/cameradirect.html in the developer documentation. They do a really good job of explaining each of the necessary steps for creating a custom camera and has useful code stubs, too, which may be easier to parse than the entire sample project. – MattDavis Jun 26 '13 at 18:33
2

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

Elivis
  • 307
  • 1
  • 6
  • 18
1

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

It releases the camera when the app dies.

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
0

Well i was searching for similar answer, i found this post below which could solve the issue

Ideal way to set global uncaught exception Handler in Android

You could release the camera at the UncaughtException

Community
  • 1
  • 1
blganesh101
  • 3,647
  • 1
  • 24
  • 44