mindless
mindless

Reputation: 51

Android take picture without SurfaceHolder

When trying to take a picture from a Service (no GUI), on some devices, I get a RuntimeException. However if I take a picture from an Activity using a SurfaceHolder, it will work.

Camera.Parameters parameters = camera.getParameters();
parameters.setPictureFormat(PixelFormat.JPEG);
camera.setParameters(parameters);
SurfaceView mview = new SurfaceView(context);
camera.setPreviewDisplay(mview.getHolder());
camera.startPreview();
camera.takePicture(null,null,iccb);

This sequence works fine on most devices but, in particular, on Motorola Droid, the call to takePicture method (actually the native method) will throw a RuntimeException. Also tried camera.setPreviewDisplay(null), but it won't work either.

However, if I use real SurfaceView from an Activity it also works fine on the Droid but I would need to do it from a background service.

Do you think that this behavior is shown due to privacy reasons so that noone can take pictures without actually displaying the image? This makes no much sense to me because I could load up a dummy activity with a 0 size preview and it works but I'd love not having to do it.

LogCat output:

10-24 12:20:57.838 D/CameraHal(267): hardware/ti/omap4/omap3/camera-omap4/src/CameraHal.cpp:189 enableMsgType - Preview callback not enabled 1c2
10-24 12:20:57.838 E/CameraHal(267): hardware/ti/omap4/omap3/camera-omap4/src/CameraHal.cpp:2448 takePicture - takePicture called with image buffer 0x0
10-24 12:20:57.838 D/AndroidRuntime(29248): Shutting down VM
10-24 12:20:57.838 W/dalvikvm(29248): threadid=1: thread exiting with uncaught exception (group=0x4001e560)
10-24 12:20:57.846 E/AndroidRuntime(29248): FATAL EXCEPTION: main
10-24 12:20:57.846 E/AndroidRuntime(29248): java.lang.RuntimeException: takePicture failed
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.native_takePicture(Native Method)
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.takePicture(Camera.java:829)
10-24 12:20:57.846 E/AndroidRuntime(29248): at android.hardware.Camera.takePicture(Camera.java:793)

Any help is very much appreciated

Upvotes: 2

Views: 4713

Answers (2)

Steven
Steven

Reputation: 1620

It seems most of the modern Android devices now check that a SurfaceView exists and has a non-zero width and height (my Samsung Galaxy SII spews warnings in such a circumstance and won't capture the photo). Interestingly if the SurfaceView has a width and height of 1dip then it works just fine.

<SurfaceView android:id="@+id/surface_view"
    android:layout_width="1dip"
    android:layout_height="1dip" />

With a width and height of 1 pixel the SurfaceView is incredibly difficult to see and whilst it works, it still falls into the category of "security issue".

Upvotes: 0

It's a security issue. You shouldn't be alowed to use the camera without hacing a preview view. This should prevent malicious use of camera without the knowledge of the user, for example for spying purposes.

Upvotes: 1

Related Questions