MonkeyBonkey
MonkeyBonkey

Reputation: 47921

android camera failed when using the camera api

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)

Upvotes: 0

Views: 3333

Answers (1)

Seidr
Seidr

Reputation: 4936

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.

Upvotes: 1

Related Questions