java.lang.RuntimeException: takePicture failed
Solution 1:
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().
-
Declare a flag as a member of your Activity:
private boolean safeToTakePicture = false;
-
In
surfaceChanged()
, just set the flag to true after calling startPreview():camera.startPreview(); safeToTakePicture = true;
-
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; }
-
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.
Solution 2:
I also had the similar issue. later i found startPreview
is very important.
_camera.startPreview()
is very important before the takePicutre checkout the point 5 and 6
in this link.
Solution 3:
There can be many reasons for this in my case i was trying to take photo without preview (hidden photo) and i was using SurfaceView
, So i replaced it with SurfaceTexture
SurfaceTexture surfaceTexture = new SurfaceTexture(10);
camera.setPreviewTexture(surfaceTexture);
and the problem was solved... P.S I was getting this error only on Above 6.0 devices
Solution 4:
Did you forget to call startPreview()
on the Camera?
See here for more information.