So this is kinda funky. If you want to take a picture on Android with the Camera you have to first create a preview surface. Seems like a bad design to me, but I don’t make the rules here. So according to the documentation you are required to call startPreview() first before calling takePicture() But what if you don’t want a preview and just want to take a picture? Well you can just create a dummy SurfaceView for the purposes of the preview and then let the garage collector grab it when you’re done. Here’s the snippet:
SurfaceView view = new SurfaceView(this);
c.setPreviewDisplay(view.getHolder());
c.startPreview();
c.takePicture(shutterCallback, rawPictureCallback, jpegPictureCallback);
Enjoy!
Update 12/6/11: Looks like this doesn’t work for every camera, I’ve added a table here summarizing the comments people have made. If you have any more to add, please send a comment for this post.
| Device | Version | Works? |
| Emulator | ? | Yes |
| Samsung Galaxy S | 2.2 | Yes |
| Motorola Droid | 2.2.2 | No |
| HTC MyTouch | 2.2-update1 | No |
| Samsung Galaxy Note | 2.3.6 | No |
| HTC Desire | 2.3.3 | Yes |

What should I put in the layout xml? I am getting an error with this snippet of code. My layout (main.xml) does not have a SurfaceView. Should I add one?
TIA
-Uma
This worked great on my Samsung Galaxy S (2.2) but I tried it out on a Motorola Droid (2.2.2) and HTC MyTouch (2.2-update1) and had problems with both. Neither will take a photo without a real preview surface. Have you had any luck with this on various OS versions?
Thanks!
Sadly, doesn’t work for me.
I never get the callback jpegPictureCallback.
I’m seeing the same issue. The callback is not run. Logcat complains about a call to a dead camera.
BUT: When I put a breakpoint right after calling takePicture, the application has enough time to take the picture, convert it and call the callback method.
I couldn’t find out yet how to set up timing correctly.
Seems that this doesn’t work on all devices. I get a black image, and also found others reporting the same behaviour. On the emulator however it is reported to work.
I’v been having this same problem. I put in my code a Thread.sleep(1000) before take a picture, and it works. it started to take good pictures.. I think the devise doesn’t have enouth time to start the camera. and the result is a black picture…
[...] [...]