1. Check out our companion app, Forums for Android! Download from Google Play

Camera Preview api demo

Discussion in 'Application Development' started by android88, Jan 6, 2010.

  1. android88

    android88 Member
    Thread Starter
    15

    Jan 6, 2010
    17
    0
    15
    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):
    1. <uses-permission android:name="android.permission.CAMERA" />
    and
    Code (Text):
    1. <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):
    1.  
    2.  
    3.  
    4. package nl.example.camerapreview;
    5.  
    6. import android.app.Activity;
    7. import android.content.Context;
    8. import android.hardware.Camera;
    9. import android.os.Bundle;
    10. import android.view.SurfaceHolder;
    11. import android.view.SurfaceView;
    12. import android.view.Window;
    13. import java.io.IOException;
    14.  
    15. // ----------------------------------------------------------------------
    16.  
    17. public class CameraPreview extends Activity {    
    18.     private Preview mPreview;
    19.  
    20.     @Override
    21.         protected void onCreate(Bundle savedInstanceState) {
    22.         super.onCreate(savedInstanceState);
    23.         System.out.println("hoera");
    24.         // Hide the window title.
    25.         requestWindowFeature(Window.FEATURE_NO_TITLE);
    26.  
    27.         // Create our Preview view and set it as the content of our activity.
    28.         try{
    29.             mPreview = new Preview(this);
    30.             setContentView(mPreview);
    31.             System.out.println("hoera");
    32.             }
    33.         catch(RuntimeException e){
    34.             System.out.println(e.getMessage());
    35.         }
    36.     }
    37.  
    38. }
    39.  
    40. // ----------------------------------------------------------------------
    41.  
    42. class Preview extends SurfaceView implements SurfaceHolder.Callback {
    43.     SurfaceHolder mHolder;
    44.     Camera mCamera;
    45.  
    46.     Preview(Context context) {
    47.         super(context);
    48.  
    49.         // Install a SurfaceHolder.Callback so we get notified when the
    50.         // underlying surface is created and destroyed.
    51.         mHolder = getHolder();
    52.         mHolder.addCallback(this);
    53.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    54.     }
    55.  
    56.     public void surfaceCreated(SurfaceHolder holder) {
    57.         // The Surface has been created, acquire the camera and tell it where
    58.         // to draw.
    59.         mCamera = Camera.open();
    60.         try {
    61.            mCamera.setPreviewDisplay(holder);
    62.         } catch (IOException exception) {
    63.             mCamera.release();
    64.             mCamera = null;
    65.             // TODO: add more exception handling logic here
    66.         }
    67.     }
    68.  
    69.     public void surfaceDestroyed(SurfaceHolder holder) {
    70.         // Surface will be destroyed when we return, so stop the preview.
    71.         // Because the CameraDevice object is not a shared resource, it's very
    72.         // important to release it when the activity is paused.
    73.         mCamera.stopPreview();
    74.         mCamera.release();
    75.         mCamera = null;
    76.     }
    77.  
    78.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    79.         // Now that the size is known, set up the camera parameters and begin
    80.         // the preview.
    81.         Camera.Parameters parameters = mCamera.getParameters();
    82.         parameters.setPreviewSize(w, h);
    83.         mCamera.setParameters(parameters);
    84.         mCamera.startPreview();
    85.     }
    86.  
    87. }
     

    Advertisement

  2. eclipsed4utoo

    eclipsed4utoo Well-Known Member
    43

    Nov 12, 2009
    435
    27
    43
    Software Developer
    Florence, SC
    can you post your manifest file?
     
  3. android88

    android88 Member
    Thread Starter
    15

    Jan 6, 2010
    17
    0
    15
    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
    25

    Oct 6, 2009
    8
    0
    25
    Yep it needs to be outside the application tags, that one stung me when I was starting too :)
     

Share This Page

Loading...