无需重启即可发布Android Camera?

发布于 2024-12-15 21:30:10 字数 287 浏览 1 评论 0原文

我目前正在尝试让相机在我的应用程序中工作,但我遇到了整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你丑哭了我 2024-12-22 21:30:10

相机资源管理是由android相机服务(libcameraservice.so)完成的。
运行Camera Service的进程是mediaserver。如果重新启动媒体服务器,您将有效地释放所有摄像机锁定。

您必须具有 adb root 访问权限才能执行此操作。

运行adb shell; su 获取 root 命令提示符。

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

媒体服务器现在应该自动启动。

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.

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.

征棹 2024-12-22 21:30:10

我不确定是否有办法通过终端或 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'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.

枕梦 2024-12-22 21:30:10

我找到了解决这个问题的方法。设置----->应用程序------>管理应用程序----->相机-->清除数据,问题就解决了

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

明媚如初 2024-12-22 21:30:10

这是安装 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.

梦言归人 2024-12-22 21:30:10

好吧,我正在寻找类似的答案,我发现下面的这篇文章可以解决问题

在 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文