0

The code below is my Android Camera API implementation, and as you can see i commented out the button/onClickListener bit. This results in the error 'takePicture Failed' during runtime. But when I use the button (and really nothing else) it works fine. (shows preview, button works, image is saved to where I want it). However I would really like to do this without a button (take picture as soon as the activity is launched). What's going wrong here?

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

    if(checkCameraHardware(this)) {
        mCamera = getCameraInstance();
        mPreview = new CameraPreview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mPreview);
        mCamera.takePicture(null, null, mPicture);
        mCamera.release();
        // Add a listener to the Capture button
        /*Button captureButton = (Button) findViewById(R.id.button_capture);
        captureButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // get an image from the camera
                    mCamera.takePicture(null, null, mPicture);
                    mCamera.release();
                }
            }
        );
        */
        //mCamera.takePicture(null, null, mPicture);
        //mCamera.release();
    }
}
Arthelais
  • 606
  • 1
  • 7
  • 17

1 Answers1

0

you can try override the activity life cycle function onResume() where you can write the code for taking picture, if it helps.

Ronn Wilder
  • 1,228
  • 9
  • 13
  • It instead just crashes there if I do that. – Arthelais Jun 06 '14 at 11:43
  • well that was strange. there's one more thing which you can do. you can make the thread to sleep for some milliseconds and then call the function to take the picture. – Ronn Wilder Jun 06 '14 at 11:50
  • Doesn't seem to solve it either, just delays the error as can be expected. – Arthelais Jun 06 '14 at 12:20
  • I tried button.performClick() function to programatically click a button to set the text of textview and it works. I am not sure of your app. as it is showing errors every time but you can give it a try. – Ronn Wilder Jun 07 '14 at 05:28