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>
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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想通了。事实证明,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.
停止cameraPreview后释放相机对象。
试试这个
After stop the cameraPreview release camera object.
Try this