Hello,

I'm building this app, which requires a Camera, but it is not working, it doesn't show anything except the design, here's teh code:

Camera c = null;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qrreaderscreen);
        SurfaceView v = (SurfaceView) findViewById(R.id.v);
        SurfaceHolder surface_holder = null;

        v = new SurfaceView(getApplicationContext());

        if (surface_holder == null) {
            surface_holder = v.getHolder();
        }

        //surface_holder.addCallback((Callback) this);
        //surface_holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);

        try {
            c = Camera.open();
            c.setPreviewDisplay(surface_holder);
            c.startPreview();
        }
        catch (Exception e){
            System.out.println("gasgasga");
        }
    }

            public void surfaceDestroyed(SurfaceHolder holder) {
                  c.stopPreview();
                  c.release();
                  c = null;
            }

            public void surfaceCreated(SurfaceHolder holder) {
                c = Camera.open();

                  try {
                       c.setPreviewDisplay(holder);  
                  } catch (IOException exception) {  
                        c.release();  
                        c = null;  
                  }
            }
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
                c.startPreview();
            }

Any help would be appreciated.

Do you have in your AndroidManifest.xml declaration of camera usage?

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

Yes i do have that :

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.eaple.cameraplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.CAMERA" />
 <uses-feature android:name="android.hardware.camera" />
 <uses-feature android:name="android.hardware.camera.autofocus" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Is it in the wrong place?

Thanks for the answer.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.