java W/CameraBase:连接到相机时发生错误:0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29996819/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:15:55  来源:igfitidea点击:

W/CameraBase﹕ An error occurred while connecting to camera: 0

javaandroidcameraruntime-errorandroid-camera

提问by Aggressor

There was a similar question but it went unresolved:

有一个类似的问题,但没有解决:

W/CameraBase﹕ An error occurred while connecting to camera: 0 on camera.open() call

W/CameraBase﹕连接到相机时发生错误:0 on camera.open() 调用

I have an app which (naturally) is using the camera. Its been working fine. However with no code changes, between builds, the app started crashing (verified no code changes via Git commit history).

我有一个(自然地)使用相机的应用程序。它一直工作正常。然而,由于没有代码更改,在构建之间,应用程序开始崩溃(通过 Git 提交历史验证没有代码更改)。

To further verify the issue is somewhere in the hardware, I ran the same code on another phone and it works just fine. Somehow the camera is locked and won't open.

为了进一步验证问题出在硬件的某个地方,我在另一部手机上运行了相同的代码,并且运行良好。不知何故,相机被锁定,无法打开。

Here is the relevant code:

这是相关的代码:

Create a previe object

创建预览对象

_cameraPreview = new CameraPreview(getActivity(),_camera);

This is the creation code for it

这是它的创建代码

 public CameraPreview(Context context, Camera camera) 
    {
        super(context);
        _camera = camera;
        _surfaceHolder = getHolder();
        _surfaceHolder.addCallback(this);
    }

Opening the camera:

打开相机:

//Opens back facing camera by default

//默认开启后置摄像头

public static Camera getCameraInstance()
{
    Camera c = null;
    try
    {
        c = Camera.open();
    }catch (Exception e)
    {
       e.printStackTrace();
    }

    return c;
}

This is where the error occurs, camera.Open throws the error with the following stack trace. My camera object is null. This code is from the official Android docs but they don't talk about how to 'release the camera' if its locked from outside the session.

这是发生错误的地方,camera.Open 使用以下堆栈跟踪引发错误。我的相机对象为空。此代码来自官方 Android 文档,但他们没有讨论如果从会话外部锁定如何“释放相机”。

The full log out is

完整的注销是

W/CameraBase﹕ An error occurred while connecting to camera: 0
W/System.err﹕ java.lang.RuntimeException: Fail to connect to camera service
W/System.err﹕ at android.hardware.Camera.<init>(Camera.java:497)
W/System.err﹕ at android.hardware.Camera.open(Camera.java:357)
W/System.err﹕ at co.pumpup.app.EditPhotoFragment.getCameraInstance(EditPhotoFragment.java:241)
W/System.err﹕ at co.pumpup.app.EditPhotoFragment.onCreateView(EditPhotoFragment.java:76)
W/System.err﹕ at android.app.Fragment.performCreateView(Fragment.java:2053)
W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
W/System.err﹕ at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
W/System.err﹕ at android.app.BackStackRecord.run(BackStackRecord.java:834)
W/System.err﹕ at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
W/System.err﹕ at android.app.Activity.performStart(Activity.java:6005)
W/System.err﹕ at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
W/System.err﹕ at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
W/System.err﹕ at android.app.ActivityThread.access0(ActivityThread.java:151)
W/System.err﹕ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
W/System.err﹕ at android.os.Looper.loop(Looper.java:135)
W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5254)
W/System.err﹕ at java.lang.reflect.Method.invoke(Native Method)
W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:372)
W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

Can you advise how I can 'unlock' a locked up camera like this?

你能告诉我如何“解锁”像这样锁定的相机吗?

回答by panonski

The "fail to connect to camera service" means that your (or some other) camera app has failed to release camera properly. This was happening to me occasionally as I was developing my app, too. The first thing I'd try is to open the camera from the default camera app that comes with the phone (just tapping the "camera" button). If that would fail to open, then the only option was to restart the phone.

“无法连接到相机服务”意味着您(或其他)相机应用程序未能正确释放相机。在我开发我的应用程序时,这也偶尔发生在我身上。我要尝试的第一件事是从手机附带的默认相机应用程序中打开相机(只需点击“相机”按钮)。如果无法打开,那么唯一的选择就是重启手机。

This is speculation, but your code might have a bug that surfaces only occasionally and it just didn't appear until now through pure luck. If it persist, check out the questions related to "how to release camera properly in android". They had helped me to deal with this.

这是推测,但您的代码可能有一个仅偶尔出现的错误,并且直到现在纯粹靠运气才出现。如果它仍然存在,请查看与“如何在 android 中正确释放相机”相关的问题。他们帮助我解决了这个问题。

回答by Andrea Motto

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen. http://developer.android.com/training/permissions/requesting.html

从 Android 6.0(API 级别 23)开始,用户在应用运行时授予应用权限,而不是在安装应用时授予权限。这种方法简化了应用安装过程,因为用户在安装或更新应用时不需要授予权限。它还使用户可以更好地控制应用程序的功能;例如,用户可以选择让相机应用程序访问相机而不是设备位置。用户可以随时通过转到应用程序的设置屏幕来撤销权限。http://developer.android.com/training/permissions/requesting.html

回答by John Smith

I don't know if this is your case but it just happened to me so I thought I'd share it. My app was doing the same thing (although I had a try surrounding Camera.open() so I didn't see it at first) BUT I did what panonski said and turned the camera on from the system camera app and saw that it was using the front facing camera. Switching it back to the rear camera allowed my app to work again.

我不知道这是否是你的情况,但它只是发生在我身上,所以我想我会分享它。我的应用程序也在做同样的事情(虽然我尝试了 Camera.open() ,所以我一开始没有看到它)但是我按照 panonski 所说的做了,并从系统相机应用程序打开了相机,看到它是使用前置摄像头。将其切换回后置摄像头让我的应用程序再次运行。

I changed from using Camera.open() to Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK) and now this works no matter how the camera was left before. (Using Galaxy Note 4).

我从使用 Camera.open() 更改为 Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK) 现在无论之前相机如何放置都可以使用。(使用 Galaxy Note 4)。

Hopefully this helps.

希望这会有所帮助。