Android 错误:无法在 Camera.open() 上连接到相机服务?

发布于 2024-11-26 03:38:10 字数 2101 浏览 0 评论 0原文

手机: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>

Phone: HTC Incredible 2 with Gingerbread 2.3.3

In my code, I am doing:

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

In the constructor of an Engine

I also have:

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

and

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

My manifest:

<?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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

玻璃人 2024-12-03 03:38:10

我想通了。事实证明,Camera.open() 调用必须在活动中进行,而不是在引擎中进行。

仍然不确定为什么会出现这种情况。如果有人能向我解释这种现象,我将不胜感激。

I figured it out. Turns out the Camera.open() call has to be made in the Activity, not in the Engine.

Still not sure why this is the case. I would appreciate if someone can explain this phenomenon to me.

執念 2024-12-03 03:38:10

停止cameraPreview后释放相机对象。

试试这个

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

After stop the cameraPreview release camera object.

Try this

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