开发者

Android error: Fail to connect to camera service on Camera.open()?

开发者 https://www.devze.com 2023-03-23 09:07 出处:网络
Phone: HTC Incred开发者_运维技巧ible 2 with Gingerbread 2.3.3 In my code, I am doing: cam = Camera.open(0);

Phone: HTC Incred开发者_运维技巧ible 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>


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.


After stop the cameraPreview release camera object.

Try this

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号