greenie
greenie

Reputation: 1740

Take and save picture on button press

I'm creating an Android application which uses user captured images as part of a larger process. So far my XML layout has a SurfaceView and Button inside a RelativeLayout. I've managed to get the camera preview to show on the SurfaceView but I'm stuck on how to take a picture and save it when a user presses the button.

My class file looks something like the CameraPreview API demo: http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/CameraPreview.html

Ideally, when the button it pressed the camera should autofocus, snap a picture (with the clicky sound), save it to /data/data/app_package_structure/files/file_name.jpg, then pop up a Toast to tell the user their image has been saved.

Any help is much appreciated :)

Upvotes: 10

Views: 24793

Answers (2)

Mikael Ohlson
Mikael Ohlson

Reputation: 3274

I think CommonsWare has really already answered most of this question, but this might work for the auto focus and the shutter sound. This is a guess, since I'm not at a machine where I can compile/test any of this.

In your button-press-handling code, I believe you should call (possibly by message passing)

camera.autoFocus(new Camera.AutoFocusCallback() {
  Camera.ShutterCallback shutterCallback = new Camera.ShutterCallback() {
    public void onShutter() {
      // Play your sound here.
    }
  };
  public void onAutoFocus(boolean success, Camera camera) {
    camera.takePicture(shutterCallback, null, photoCallback);
  }
});  

where camera is your camera object, and photoCallback is the same as in CommonsWare's example.

Exactly what is it that you are stuck on?

Oh, and don't forget to add the <uses-feature> tag android.hardware.camera.autofocus. :)

Upvotes: 12

CommonsWare
CommonsWare

Reputation: 1007533

Here is a sample application that handles the take-a-picture-and-save-it part. Auto-focus, clicky, Toast, and saving to the app-local file store vs. the SD card are left as exercises for the student. :-)

Upvotes: 9

Related Questions