• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Camera Preview api demo

android88

Newbie
Jan 6, 2010
17
0
Hello everyone. I'm completely new to android, so hopefully someone can help me with this :)

I'm trying to run the Camera Preview api demo from the android website emulator (set to android 1.5), but i keep getting error the "application has stopped unexpectedly" error. Logcat says the following:

01-05 21:13:24.550: WARN/ServiceManager(542): Permission failure: android.permission.CAMERA from uid=10035 pid=867
01-05 21:13:24.561: ERROR/CameraService(542): Permission Denial: can't use the camera pid=867, uid=10035
01-05 21:13:24.580: DEBUG/AndroidRuntime(867): Shutting down VM
01-05 21:13:24.590: WARN/dalvikvm(867): threadid=3: thread exiting with uncaught exception (group=0x4001aa28)
01-05 21:13:24.590: ERROR/AndroidRuntime(867): Uncaught handler: thread main exiting due to uncaught exception
01-05 21:13:24.610: ERROR/AndroidRuntime(867): java.lang.RuntimeException: Fail to connect to camera service

From what i read online, the permission error should be fixed by adding a line to the manifest, but that doesn't seem to work. I tried adding two different lines:
Code:
<uses-permission android:name="android.permission.CAMERA" />
and
Code:
<uses-permission android:name="android.permission.CAMERA"></uses-permission>
But for both i still get the error. Does anyone see my problem?

Below is the main code. I didn't change anything in the layout file, and i only added the permission line to the manifold.

Code:
package nl.example.camerapreview;

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import java.io.IOException;

// ----------------------------------------------------------------------

public class CameraPreview extends Activity {    
    private Preview mPreview;

    @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        System.out.println("hoera");
        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        // Create our Preview view and set it as the content of our activity.
        try{
            mPreview = new Preview(this);
            setContentView(mPreview);
            System.out.println("hoera");
            }
        catch(RuntimeException e){
            System.out.println(e.getMessage());
        }
    }

}

// ----------------------------------------------------------------------

class Preview extends SurfaceView implements SurfaceHolder.Callback {
    SurfaceHolder mHolder;
    Camera mCamera;

    Preview(Context context) {
        super(context);

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        mCamera = Camera.open();
        try {
           mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
            // TODO: add more exception handling logic here
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Because the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }

}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones