5

I've implemented a service to take a picture from a background thread, but the photo never gets taken on any of my devices... here is the code (logging output below):

public class PhotoCaptureService extends Service {
    private static final String TAG = "PhotoCaptureService";

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.d(TAG, "Starting the PhotoCaptureService");
        takePhoto();
    }

    private void takePhoto() {

        Log.d(TAG, "Preparing to take photo");
        Camera camera = null;

        try {

            camera = Camera.open();

        } catch (RuntimeException e) {

            Log.e(TAG, "Camera not available", e);
            return;
        }

        if (null == camera) {

            Log.e(TAG, "Could not get camera instance");
            return;
        }

        Log.d(TAG, "Got the camera, creating the dummy surface texture");
        SurfaceTexture dummySurfaceTexture = new SurfaceTexture(0);

        try {

            camera.setPreviewTexture(dummySurfaceTexture);

        } catch (Exception e) {

            Log.e(TAG, "Could not set the surface preview texture", e);
        }

        Log.d(TAG, "Preview texture set, starting preview");

        camera.startPreview();

        Log.d(TAG, "Preview started");

        camera.takePicture(null, null, new Camera.PictureCallback() {

            @Override
            public void onPictureTaken(byte[] data, Camera camera) {

                Log.d(TAG, "Photo taken, stopping preview");

                camera.stopPreview();

                Log.d(TAG, "Preview stopped, releasing camera");

                camera.release();

                Log.d(TAG, "Camera released");
            }
        });
    }

Logging output:

D/PhotoCaptureService﹕ Starting the PhotoCaptureService
D/PhotoCaptureService﹕ Preparing to take photo
D/PhotoCaptureService﹕ Got the camera, creating the dummy surface texture
D/PhotoCaptureService﹕ Preview texture set, starting preview
D/PhotoCaptureService﹕ Preview started

At this point nothing else happens, the onPictureTaken method never gets called and there is no error or exception thrown. Does anyone know why this is happening? I've looked at every camera tutorial on StackOverflow and nothing seems to work.

Joshua W
  • 4,973
  • 5
  • 24
  • 30
  • 1. Service does not run in background thread. To use desired background service you must extend IntentService. 2. Please, provide us with stack trace from the exception. – dawid gdanski Jan 30 '14 at 18:04
  • this is the kind of stack trace he might be getting, as i have the same problem – zeshanbaig786 Feb 21 '14 at 18:57
  • @dawidgdanski, 1. I've been able to take photos from a regular (not `IntentService`) service in the main thread. 2. He said there isn't an exception thrown. – Sam Nov 22 '14 at 23:02
  • @Sam maybe this could be helpful https://github.com/dawidgdanski/AndroidComponentsTests/tree/master/src/com/test/AndroidComponentsTests/media the examples start with CaptureImage... – dawid gdanski Nov 23 '14 at 10:36
  • Another example from Mr Mark Murphy: https://github.com/commonsguy/cw-advandroid/blob/master/Camera/Picture/src/com/commonsware/android/picture/PictureDemo.java – dawid gdanski Nov 23 '14 at 10:56

1 Answers1

1

From my experience and what I've read, the dummy SurfaceTexture strategy doesn't work on all phones. Try instead adding a 1x1 pixel SurfaceView and starting the preview in the SurfaceView.getHolder()'s onSurfaceCreated callback (added via addCallback).

See Taking picture from camera without preview for more information.

Community
  • 1
  • 1
Sam
  • 40,644
  • 36
  • 176
  • 219