Android - 无法连接到相机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2563973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - Fail to connect to camera
提问by teepusink
I'm using the Android APIDemo sample code.
我正在使用 Android APIDemo 示例代码。
When I run the CameraPreview example, at first it was giving me an error.
当我运行 CameraPreview 示例时,起初它给了我一个错误。
I traced that one down and the sample was working for a while.
Now, it no longer works. It says
我追踪了那个,样本工作了一段时间。
现在,它不再起作用了。它说
ERROR/AndroidRuntime(2949): java.lang.RuntimeException: Fail to connect to camera service
What can be causing that? It happens when camera.open()is called.
是什么原因造成的?它发生在camera.open()被调用时。
Thanks,
Tee
谢谢,
Tee
回答by Kangur
Be sure to properly release all the aquired camera resources:
确保正确释放所有获得的相机资源:
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (mCam != null) {
mCam.stopPreview();
mCam.setPreviewCallback(null);
mCam.release();
mCam = null;
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (mCam == null) {
mCam = Camera.open();
try {
mCam.setPreviewDisplay(holder);
// TODO test how much setPreviewCallbackWithBuffer is faster
mCam.setPreviewCallback(this);
} catch (IOException e) {
mCam.release();
mCam = null;
}
}
}
回答by CommonsWare
Make sure your <uses-permission>elements are in the proper positionsin your AndroidManifest.xmlfile.
确保您的<uses-permission>元素位于文件中的正确位置AndroidManifest.xml。
回答by plus-
It happens if your activity does not close the camera properly in surfaceDestroyedor onConfigurationChangedetc...
它发生,如果你的活动不正常关闭相机surfaceDestroyed或onConfigurationChanged等...
Don't forget to do this everytime you go out of your activity:
每次离开活动时不要忘记这样做:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
回答by Sharj
Another reason of this error is when you try to open camera but some other application or even your application is alreadyusing camera.
此错误的另一个原因是当您尝试打开相机但某些其他应用程序甚至您的应用程序已经在使用相机时。
回答by PankajAndroid
I also get this type of issue on a HTC device. To solve add this code:
我也在 HTC 设备上遇到这种类型的问题。要解决添加此代码:
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
if (camera!=null)
{
camera.stopPreview();
camera.release();
camera=null;
}
}
And yet you cannot start camera then restart device.
但是您无法启动相机然后重新启动设备。
回答by wizurd
Also, if you are using the emulator, make sure you have selected to Emulate the Front Camera and/or the Back Camera.
此外,如果您使用的是模拟器,请确保您已选择模拟前置摄像头和/或后置摄像头。
Android Virtual Device Manager->Select Device->Edit->Front Camera->Emulated
Android虚拟设备管理器->选择设备->编辑->前置摄像头->模拟
回答by matt burns
As others mention, you have to call release() on your camera object when you're finished.
正如其他人提到的,完成后您必须在相机对象上调用 release() 。
I wasn't doing this initially, so I changed my code but it still gave me the same error. I was deploying directly to a physical handset and had to restart the phone before it worked
我最初没有这样做,所以我改变了我的代码,但它仍然给了我同样的错误。我直接部署到物理手机上,必须重新启动手机才能工作
回答by birdman
I also received this error when I was testing and stopped execution before reaching the point in code when the:
当我测试并在到达代码点之前停止执行时,我也收到了这个错误:
if (camera!=null){
camera.stopPreview();
camera.release();
camera=null;
}
was called. This then blocked the camera because it hadn't een released properly. My solution was to turn the camera off and back on again. You can confirm this is the case by trying to use the inbuilt Camera app in your phone. It won't work either because it is still busy.
被称为。这然后挡住了相机,因为它没有正确释放。我的解决方案是关闭相机并重新打开。您可以通过尝试使用手机中的内置相机应用程序来确认是这种情况。它也不会工作,因为它仍然很忙。
回答by Brian
Second @matt-burns however you might want to check that you're only trying to get the camera once. I had forgotten to comment out a line and was trying to launch two activities that would both try to obtain the camera.
第二个@matt-burns 但是你可能想检查一下你是否只试图拿到相机一次。我忘记注释掉一行,并试图启动两个都试图获取相机的活动。

