Java.Lang.Runtimeexception: Takepicture Failed

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.

java.lang.RuntimeException: takePicture failed in lollipop and marshmallow

Try this

Add:

SurfaceTexture st = new SurfaceTexture(MODE_PRIVATE); 
pCamera.setPreviewTexture(st);

before:

pCamera.startPreview();

takePicture failed

Hope this will help u out

Android: java.lang.RuntimeException: takePicture failed

Assuming that you want the jpeg saved, you don't need raw callback, so
change the call to takePicture() from:

mCamera.takePicture(null, mPicture, mPicture);

to

mCamera.takePicture(null, null, mPicture);

in your onTouchEvent(). This will use jpeg picture callback.

Calling takePicture() also causes the preview to stop, so you might want to call startPreview() again in your onPictureTaken() callback, if you want to take more pictures or have a preview restarted.

Also, in your onTouchEvent(), you might be getting multiple events, so filter for the one that works for you:

@Override
public boolean onTouchEvent(MotionEvent event) {
Toast.makeText(context, "Picture taken", Toast.LENGTH_SHORT).show();
//System.gc(); tried this because it was suggested in a stackoverflow question but it didn't help.

switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// A pressed gesture has started, the motion contains the initial starting location
break;

case MotionEvent.ACTION_UP:
// A pressed gesture has finished, the motion contains the final release location
// as well as any intermediate points since the last down or move event.

mCamera.takePicture(null, mPicture, mPicture);

break;

default:
break;
}

return true; //processed
}

If you place takePicture() in ACTION_DOWN, it will be called as soon as you touch the screen, whereas when in ACTION_UP, it will happen when you remove finger. See which one works for you.



Related Topics



Leave a reply



Submit