Android错误:在camera .open()上连接相机服务失败



手机:HTC Incredible 2 with Gingerbread 2.3.3

在我的代码中,我做的是:

cam = Camera.open(0);
SurfaceHolder surfaceHolder = getSurfaceHolder();
try
{
    cam.setPreviewDisplay(surfaceHolder);
    cam.startPreview();
}
catch (IOException e)
{
    e.printStackTrace();
}

引擎的构造函数

我还有:

@Override
public void onDestroy()
{
    super.onDestroy();
    if (cam != null)
    {
        cam.stopPreview();
        cam.setPreviewCallback(null);
        cam.release();
        cam = null;
    }
}

@Override
public void onSurfaceDestroyed(SurfaceHolder holder)
{
    super.onSurfaceDestroyed(holder);
    if (cam != null)
    {
        cam.stopPreview();
        cam.setPreviewCallback(null);
        cam.release();
        cam = null;
    }
}

我的清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.google.apis" android:versionCode="1"
    android:versionName="1.0">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.software.live_wallpaper" />
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <service android:label="@string/label"
            android:name="com.me.app.main.AppName"
            android:permission="android.permission.BIND_WALLPAPER">
            <intent-filter>
                <action android:name="android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data android:name="android.service.wallpaper"
                android:resource="@xml/app" />
        </service>
        <activity
            android:name="com.me.app.main.AppName"
            android:label="@string/app_name">
            android:exported="true">
        </activity>
    </application>
</manifest>

我明白了。原来Camera.open()调用必须在Activity中进行,而不是在Engine中。

仍然不确定为什么会这样。如果有人能给我解释一下这个现象,我会很感激的。

停止cameraPreview后释放相机对象。

试试这个

stopCameraPreview(){
    if(camera != null){
        camera.stopPreview();
        camera.release();
    }
}

最新更新