Android Take Picture without Preview

Posted by Frank March - 9 - 2011 - Wednesday

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

8 Responses to “Android Take Picture without Preview”

  1. Uma Sun says:

    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

  2. Katie says:

    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!

  3. Tobias says:

    Sadly, doesn’t work for me.
    I never get the callback jpegPictureCallback.

    • Juergen says:

      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.

  4. Norbert says:

    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.

    • guthyerrz says:

      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…

  5. Eduardo says:

    Works great in HTC One! THANKS A LOT! I “only” spent a frigging afternoon on this till I found this :)

Leave a Reply

Most Recent Talk