0

I'm having trouble taking a picture in android using the camera api. Using the camera intent seems to work fine, but not when I call the api directly.

Error: java.lang.RuntimeException: takePicture failed at android.hardware.Camera.native_takePicture

Code

public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  cameraId = findFrontFacingCamera();
  camera = Camera.open(cameraId);
  Parameters params = camera.getParameters();

  SurfaceView dummy=new SurfaceView(context);
  try {
      camera.setPreviewDisplay(dummy.getHolder());
      camera.startPreview();
      camera.takePicture(null, photoHandler, photoHandler);

  } catch (IOException e) {
      camera.stopPreview();
      camera.release();
  }  
}

 private PictureCallback photoHandler = new PictureCallback() {   
      @Override
      public void onPictureTaken(byte[] data, Camera camera) {
      }
 }

Manifest

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

log cat output

07-17 10:16:02.523: D/MakePhotoActivity(14591): Camera found
07-17 10:16:02.773: D/dalvikvm(14591): threadid=1: still suspended after undo (sc=1 dc=1)
07-17 10:16:18.229: D/AndroidRuntime(14591): Shutting down VM
07-17 10:16:18.229: W/dalvikvm(14591): threadid=1: thread exiting with uncaught exception (group=0x40c4f930)
07-17 10:16:18.309: E/AndroidRuntime(14591): FATAL EXCEPTION: main
07-17 10:16:18.309: E/AndroidRuntime(14591): java.lang.RuntimeException: Unable to start activity ComponentInfo{tv.fakelove.stationtostation/tv.fakelove.stationtostation.MainActivity}: java.lang.RuntimeException: takePicture failed
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.os.Looper.loop(Looper.java:137)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.ActivityThread.main(ActivityThread.java:5041)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at java.lang.reflect.Method.invokeNative(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at java.lang.reflect.Method.invoke(Method.java:511)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at dalvik.system.NativeStart.main(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591): Caused by: java.lang.RuntimeException: takePicture failed
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.hardware.Camera.native_takePicture(Native Method)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.hardware.Camera.takePicture(Camera.java:1095)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.hardware.Camera.takePicture(Camera.java:1040)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at tv.fakelove.stationtostation.MainActivity.onCreate(MainActivity.java:59)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.Activity.performCreate(Activity.java:5104)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-17 10:16:18.309: E/AndroidRuntime(14591):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

1 Answers1

1

Check out the answer provided in a previous question: Android: "Camera.takePicture failed" Exception

Apparently you need to start the preview before taking a photo, which includes setting a valid preview surface (which I don't think you are doing).

Also, check out step 5-6 laid out at http://developer.android.com/reference/android/hardware/Camera.html

  1. Obtain an instance of Camera from open(int).
  2. Get existing (default) settings with getParameters().
  3. If necessary, modify the returned Camera.Parameters object and call setParameters(Camera.Parameters).
  4. If desired, call setDisplayOrientation(int).
  5. Important: Pass a fully initialized SurfaceHolder to setPreviewDisplay(SurfaceHolder). Without a surface, the camera will be unable to start the preview.
  6. Important: Call startPreview() to start updating the preview surface. Preview must be started before you can take a picture.
  7. When you want, call takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to capture a photo. Wait for the callbacks to provide the actual image data.
  8. After taking a picture, preview display will have stopped. To take more photos, call startPreview() again first.
  9. Call stopPreview() to stop updating the preview surface.
  10. Important: Call release() to release the camera for use by other applications. Applications should release the camera immediately in onPause() (and re-open() it in onResume()).

One thought - as you're using a dummy SurfaceView, would you need to set the width / height of the SurfaceView, to match the width / height of the preview size of the Camera?

Without the full stack trace, there's not a whole lot more I can discern that's wrong with your code.

Community
  • 1
  • 1
Seidr
  • 4,946
  • 3
  • 27
  • 39
  • added the logcat output... I do add a surfaceview to the preview and call startPreview before taking the pic... as for the dummy surface view code, I got that from http://stackoverflow.com/questions/10959767/invisible-surfaceview-for-camera-preview – MonkeyBonkey Jul 17 '13 at 15:03
  • If you're still having issues later I'll put together a replica application when I get back from work, and see if I can get it working. My gut feeling is it's something to do with the SurfaceView. Have you tried setting a 'real' Surface as the Preview Surface (i.e. one that is in the main activity layout, and has a implicit width/height)? – Seidr Jul 17 '13 at 15:12
  • As an additional..are you sure that the camera is being opened successfully? – Seidr Jul 17 '13 at 15:15
  • It looks like changing surfaceview to surfacetexture fixes the error. From what I've read it looks like surfaceview is only for older android versions? – MonkeyBonkey Jul 17 '13 at 15:17
  • Very odd especially as there are so many examples out there using SurfaceView as the dummy preview. Have you tried using a lower API / target device with the SurfaceView, to see if it'll work? Glad you managed to resolve the issue :) – Seidr Jul 17 '13 at 15:19