java.lang.RuntimeException: takePicture failed

Firstly, catch your exceptions in onPictureTaken, leaving empty catch sections is not a good practice. Then, I would add a flag that would prevent from calling takePicture() while previous picture is being saved. Later in your button onClick you would check if it’s ok to call takePicture().

  1. Declare a flag as a member of your Activity:

    private boolean safeToTakePicture = false;
    
  2. In surfaceChanged(), just set the flag to true after calling startPreview():

    camera.startPreview();
    safeToTakePicture = true;
    
  3. In your onClick() listener check the flag and take picture if ok to do so:

    if (safeToTakePicture) {
        mp.start();
        camera.takePicture(null, null, mPicture); 
        safeToTakePicture = false;
    }
    
  4. In onPictureTaken(), set the flag again to true after picture has been saved (and add exception printing):

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            pictureFile = getOutputMediaFile();
            camera.startPreview();
    
            if (pictureFile == null) {
                //no path to picture, return
                safeToTakePicture = true;
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();              //<-------- show exception
            } catch (IOException e) {
                e.printStackTrace();              //<-------- show exception
            }
    
            //finished saving picture 
            safeToTakePicture = true;
        }
    };
    

NOTES:
As the docs say, “Preview must be started before you can take a picture.”, so possible enhancement would be to use setPreviewCallback() to register callback that will be called when preview data is available, and set the flag to true when onPreviewFrame is called.

Leave a Comment