无需重启即可发布Android Camera?
我目前正在尝试让相机在我的应用程序中工作,但我遇到了整个 camera.release()
问题;我正在做相当复杂的事情,并且没有明确的方法来简单地调用camera.release(),所以我必须继续尝试新的方法,但每次我出错时,我的应用程序都会崩溃(正如预期的那样) ),使相机未释放。因此,为了能够再次测试我的应用程序,我需要重新启动手机以刷新相机。
有没有办法通过终端或DDMS释放相机,或者是唯一通过重新启动手机来释放相机?它已经很旧了,尤其是当一个小错误导致我浪费时间等待手机重启时。
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
相机资源管理是由android相机服务(libcameraservice.so)完成的。
运行Camera Service的进程是
mediaserver
。如果重新启动媒体服务器,您将有效地释放所有摄像机锁定。您必须具有 adb root 访问权限才能执行此操作。
运行adb shell; su 获取 root 命令提示符。
媒体服务器现在应该自动启动。
Android 7.0
最新版本的 Android 将
mediaserver
二进制文件拆分为多个专用二进制文件以帮助提高安全性。请参阅官方文档了解更多信息。因此,您应该寻找
cameraserver
,而不是mediaserver
。虽然没有测试过。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.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 thecameraserver
. Not tested though.我不确定是否有办法通过终端或 DDMS 重置相机,但通过在 Activity 的
中调用
方法,因此只要应用程序进入后台,相机就会被释放。同样,我的相机初始化全部发生在Camera.release()
,我取得了很大的成功onPause()onResume()
中,因此相机将在第一次运行时打开,然后在您的应用移入和移出后台时释放并重新初始化。这项技术完全消除了您为我描述的问题。如果问题仍然存在,还可以考虑将可能崩溃的代码包装在 try/catch 中,在其中捕获任何
Exception
并在该 catch 块中调用以释放相机。我通常使用这种技术来解决可以在不关闭应用程序的情况下消除的错误,但我相信它在所有情况下都适用。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'sonPause()
method, so the camera gets released whenever the App goes into the background. Similarly, my Camera initialization all happens inonResume()
, 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.我找到了解决这个问题的方法。设置----->应用程序------>管理应用程序----->相机-->清除数据,问题就解决了
I find a method to solve this problem. Setting----->Applications------>Manage Applications----->camera--->clear data ,then the problem is solved
这是安装 UncaughtExceptionHandler 的解决方案:
https://stackoverflow.com/a/17336974/755804
当应用程序终止时,它会释放相机。
Here's a solution that installs a
UncaughtExceptionHandler
:https://stackoverflow.com/a/17336974/755804
It releases the camera when the app dies.
好吧,我正在寻找类似的答案,我发现下面的这篇文章可以解决问题
在 Android 中设置全局未捕获异常处理程序的理想方法
您可以在 UncaughtException 处释放相机
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