Stack Exchange
Android - java.lang.RuntimeException: takePicture failed - Is there a solution to resolve this exception for all?
public class CameraActivity extends Activity {

     private Camera mCamera;

     private CameraPreview mPreview;

     private FrameLayout mFramePreview;

     private boolean mSafeToTakePicture;

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_camera);

         Bundle bundle = savedInstanceState;
         if (savedInstanceState == null) {
             bundle = getIntent().getExtras();
         }

         mFramePreview = (FrameLayout) findViewById(R.id.camera_preview);

         mSafeToTakePicture = false;
     }

     @Override
     protected void onResume() {
         super.onResume();

         // Create an instance of Camera
         mCamera = getCameraInstance();
         Camera.Parameters params = mCamera.getParameters();

         List<Camera.Size> supportedSizes = mCamera.getParameters().getSupportedPictureSizes();
         int max = 0;
         int index = 0;

         for (int i = 0; i < supportedSizes.size(); i++){
             Camera.Size s = supportedSizes.get(i);
             int size = s.height * s.width;
             if (size > max) {
                 index = i;
                 max = size;
             }
         }
         params.setPictureSize(supportedSizes.get(index).width,      supportedSizes.get(index).height);
         mCamera.setParameters(params);
                   mCamera.getParameters().setPictureSize(supportedSizes.get(index).width, supportedSizes.get(index).height);

         // Create our Preview view and set it as the content of our activity.
         mPreview = new CameraPreview(this, mCamera);
         mFramePreview.addView(mPreview);
         mSafeToTakePicture = true;

         mFramePreview.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View view) {
                 if (mSafeToTakePicture) {
                     takePhoto();
                     mFramePreview.setOnClickListener(null);
                     mSafeToTakePicture = false;
                 }
             }
         });
     }

     public void takePhoto() {
         mCamera.autoFocus(new Camera.AutoFocusCallback() {
             @Override
             public void onAutoFocus(boolean b, Camera pCamera) {
                 mCamera.takePicture(null, null, picture);
             }
         });
     }

     final Camera.PictureCallback picture = new Camera.PictureCallback() {
         @Override
         public void onPictureTaken(byte[] data, Camera camera) {
             /* save picture... */
         }
     };
}

An exception sometimes happens when I take a picture.

RuntimeException->"takePicture failed" (@android.hardware.Camera:native_takePicture:-2), (@android.hardware.Camera:takePicture:1833), (@android.hardware.Camera:takePicture:1778), (@fr.selic.thuit.activities.CameraActivity$2:onAutoFocus:213), (@android.hardware.Camera$EventHandler:handleMessage:1283), (@android.os.Handler:dispatchMessage:111), (@android.os.Looper:loop:207), (@android.app.ActivityThread:main:5728), (@java.lang.reflect.Method:invoke:-2), (@com.android.internal.os.ZygoteInit$MethodAndArgsCaller:run:789), (@com.android.internal.os.ZygoteInit:main:679),


We need more information, explain with more detail what the problem is and include logcat error.

Can you share the complete stacktrace ?

I edited the post, do not hesitate if you want more information

If possible, show your activity where this problem occurs. The small piece of code you provided doesn't help much. The preview must be started before you can take a picture, are you doing that first?

It is impossible for the picture to be taken before opening the preview. I added code, I hope this will help.
