无法连接到摄像机服务连接到、摄像机

2023-09-12 03:43:11 作者:荒

我试图访​​问摄像头在我的手机。我正在写一个简单的存根程序把code在一个小部件之前。我没有得到很远。在code总是抛出一个运行时异常无法连接到摄像机服务的code(从commonsware例如捏),它的错误在于:

I'm trying to access the camera on my phone. I'm writing a simple stub app prior to putting the code in a widget. I'm not getting very far. The code always throws a runtime exception "failed to connect to camera service" The code(pinched from a commonsware example) which goes wrong is:

    @Override
    public void onResume() {
        super.onResume();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
            Camera.CameraInfo info = new Camera.CameraInfo();
            for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
                Camera.getCameraInfo(i, info);
                if (info.facing == Camera.CameraInfo.CAMERA_FACING_BACK) {
                    try {
                        // Gets to here OK
                        camera = Camera.open(i);
                    } catch (Exception e) {
                        e.printStackTrace();
                        //  throws runtime exception :"Failed to connect to camera service"
                    }
                }
            }
        }
}

和我的清单是(修正10月20日)

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nbt.cameratest"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="9" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".CameraTestActivity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

            <uses-permission android:name="android.permission.CAMERA" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    </application>
</manifest>

任何人都可以请建议可能是什么问题?

Can anyone please suggest what might be wrong?

更新10月20日

LogCat中的SDK 4.0被打破,不会显示在日志的末尾,所以我剪了此位是最好的,我可以从命令行亚行logcat:

Logcat in SDK 4.0 is broken and wont show the end of the log, so I've cut this bit as best as I can from command line adb logcat:

W/ServiceManager( 2588): Permission failure: android.permission.CAMERA from uid=10136 pid=5744
E/CameraService( 2588): Permission Denial: can't use the camera pid=5744, uid=10136
W/System.err( 5744): java.lang.RuntimeException: Fail to connect to camera service
W/System.err( 5744):    at android.hardware.Camera.native_setup(Native Method)
W/System.err( 5744):    at android.hardware.Camera.<init>(Camera.java:294)
W/System.err( 5744):    at android.hardware.Camera.open(Camera.java:255)
etc..

我不知道为什么我没有权限,因为它是在清单中声明

I don't know why I haven't permission as it is declared in the manifest

推荐答案

几件事情:

为什么在你的活动标签的使用,权限和使用,功能标签。一般而言,权限包括在你的&LT的直接子;清单&GT; 标记。这可能是问题的一部分。

Why are your use-permissions and use-features tags in your activity tag. Generally, permissions are included as direct children of your <manifest> tag. This could be part of the problem.

据android的相机打开文档,运行时抛出异常:

According to the android camera open documentation, a runtime exception is thrown:

如果连接到摄像机服务失败(例如,如果摄像头正由另一个进程或设备的策略管理器的使用已禁用摄像头)

if connection to the camera service fails (for example, if the camera is in use by another process or device policy manager has disabled the camera)

如果有摄像头正被别的东西,或者你尝试检查,如果你的策略管理器有一定的环境,相机被关闭?

Have you tried checking if the camera is being used by something else or if your policy manager has some setting where the camera is turned off?

不要忘了&LT;使用特征的android:名字=android.hardware.camera.autofocus/&GT; 自动对焦

虽然我不知道,如果任何这些会直接帮你,我想他们是值得研究的,如果没有其他原因,而不是简单地排除。尽职调查,如果你会的。

While I'm not sure if any of these will directly help you, I think they're worth investigating if for no other reason than to simply rule out. Due diligence if you will.

编辑 正如下面的意见,解决办法是将使用-权限到应用程序标签之上。

EDIT As mentioned in the comments below, the solution was to move the uses-permissions up to above the application tag.