Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Fail to connect to camera service at Camera.open();

There are many questions similar to this, but none helped me:

my manifest file is:

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

my Activity class is:

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;
    }
}

i get the Exception at the line

myCamera = Camera.open();

Thank You

like image 737
Archie.bpgc Avatar asked Oct 03 '12 09:10

Archie.bpgc


2 Answers

Forgot to add

myCamera.release();

in my code. hence it works for the first time i launch the application. When i re-launch it the Camera service is not available.

like image 184
Archie.bpgc Avatar answered Sep 30 '22 20:09

Archie.bpgc


You will need to add the following

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

Hope this helps!

like image 32
Royston Pinto Avatar answered Sep 30 '22 20:09

Royston Pinto