Lost
Lost

Reputation: 13565

Camera.Open does not open camera

I am trying to open camera applicaiton using following code:

 Camera camera = Camera.open();
 Parameters p = camera.getParameters();
 p.setFlashMode(Parameters.FLASH_MODE_ON);
 camera.setParameters(p);
 camera.startPreview();        
 camera.release();

It does not throw any error but it does not open the camera either. I tried it both with and without camera.release() option. Is there anything that I am doing wrong?

Upvotes: 0

Views: 4373

Answers (1)

frogmanx
frogmanx

Reputation: 2630

Make sure relevant permissions exist in your manifest.

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

According to google Dev:

Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.

Make sure your Activity implements SurfaceHolder.Callback

Camera camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_ON);
camera.setParameters(p);
SurfaceView surfaceView = (SurfaceView)findViewById(R.id.surfaceView1); //add this to your xml view
SurfaceHolder surfaceHolder = surfaceView.getHolder()
surfaceHolder.addCallback(this);
camera.setPreviewDisplay(surfaceHolder);
camera.startPreview();      

Alternatively, if you just want to open any Camera application, use:

Intent newCameraApp = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(newCameraApp , 1337);

Upvotes: 4

Related Questions