Android:无法连接到Camera.open()的相机服务;

时间:2012-10-03 09:09:07

标签: android android-camera

有许多类似的问题,但没有人帮助我:

我的清单文件是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.cameraapi"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".CameraAPIActivity"
            android:label="@string/app_name"
            android:screenOrientation="landscape" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

我的Activity类是:

public class CameraAPIActivity extends Activity {
    private Camera myCamera = null;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        if (checkCameraHardware(this)) {

            try {

                myCamera = Camera.open();
            } catch (Exception e) {

                //Here i get the Exception:  Failed to connect to camera service
            }
        }
}

private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(
            PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}

我在

行获得了例外
myCamera = Camera.open();

谢谢

4 个答案:

答案 0 :(得分:13)

忘记添加

myCamera.release();

在我的代码中。因此它第一次启动应用程序。当我重新启动它时,相机服务不可用。

答案 1 :(得分:2)

您需要添加以下内容

private Preview mPreview;      // Global variable
mPreview = new Preview(this);  // onCreate()
setContentView(mPreview);      // onCreate()

希望这有帮助!

答案 2 :(得分:0)

您在源文件的顶部导入了错误的相机类(我沉没了),即android.graphics.Camera

您需要android.hardware.Camera

后:

myCamera = Camera.open(); - start working.

答案 3 :(得分:-5)

如果您想从相机中拍摄照片

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);`

还添加此功能

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    }
}

imageView是您可能想要设置捕获图像的视图。