1. 2015 is going to be a great year for Android! Why wait??
    Samsung Galaxy Note 5 | Samsung Galaxy S6 | HTC One M9
  2. New Forum Created: Samsung Gear VR!

Camera Preview api demo


  1. android88

    android88 Member

    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 (Text):
    <uses-permission android:name="android.permission.CAMERA" />
    and
    Code (Text):
    <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 (Text):


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

    }

    Advertisement
    :
  2. eclipsed4utoo

    eclipsed4utoo Well-Known Member

    can you post your manifest file?
  3. android88

    android88 Member

    I just found the problem. It was in the manifest file :)
    According to a tutorial the permission line needed to be within the <application> </application>, but it needed to be outside it.
    Now it works :)
    thanks for the reply
  4. draffodx

    draffodx Member

    Yep it needs to be outside the application tags, that one stung me when I was starting too :)
Loading...

Share This Page