PGMacDesign
PGMacDesign

Reputation: 6312

How do I take a photo in the onCreate method in Android instead of onClick?

I am working on a project that takes a photo. It is currently working fine when I leave it as is, but when I try to change it so that the picture is attempted to be taken in a different method (rather than being activated by an onClick() method), it errors out every time. Currently the WORKING code is as follows:

public class Testing123 extends Activity {

    private SurfaceView preview=null;
    private SurfaceHolder previewHolder=null;
    private Camera camera=null;
    private boolean inPreview=false;
    private boolean cameraConfigured=false;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        preview = (SurfaceView)findViewById(R.id.preview);
        previewHolder = preview.getHolder();
        previewHolder.addCallback(surfaceCallback);
        previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        testing();   //HERE is where I am testing, but cannot get it to work. [See part 2]
    }

    public void onResume() {
        super.onResume();
        if (camera == null) {
            camera=Camera.open();
            L.m("Camera open in onResume");
        }
        startPreview();
    }   

    public void onPause() {
        if (inPreview) {
            camera.stopPreview();
        }
        camera.release();
        camera=null;
        inPreview=false;
        super.onPause();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        new MenuInflater(this).inflate(R.menu.options, menu);
        return(super.onCreateOptionsMenu(menu));
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.camera) {
            if (inPreview) {
                camera.takePicture(null, null, photoCallback);    //THIS IS WHERE THE PHOTO IS BEING TAKEN
                inPreview=false;
            }
        }

        return(super.onOptionsItemSelected(item));
    }

    private void initPreview(int width, int height) {
        if (camera != null && previewHolder.getSurface() != null) {
            try {
                camera.setPreviewDisplay(previewHolder);
            }
            catch (Throwable t) {
                Log.e("PreviewDemo-surfaceCallback", "Exception in setPreviewDisplay()", t);
            }

            if (!cameraConfigured) {
                Camera.Parameters parameters=camera.getParameters();
                setCameraResolution(); //Leaving this method out for now
                camera.setParameters(parameters);
                cameraConfigured=true;
            }

        }
    }

    private void startPreview() {
        camera.startPreview();
    }   

    SurfaceHolder.Callback surfaceCallback=new SurfaceHolder.Callback() {
        public void surfaceCreated(SurfaceHolder holder) {
        }

        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
            initPreview(width, height);
            startPreview();
        }

        public void surfaceDestroyed(SurfaceHolder holder) {
        }
    };

    Camera.PictureCallback photoCallback = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera camera) {
            //This part is no problem, I am able to write it to the file and use the data from the output of the camera
        }
    }

The issue comes when I try to make a new call, as you see in the onCreate() method, called testing(); Whenever I call the testing method, which is an attempt to bypass a need to press a button, it errors out. Here is the testing() code:

private void testing() {
    try{
        cameraConfigured=true; 
        inPreview=true; 
        camera=Camera.open();
        Camera.Parameters parameters=camera.getParameters();
        setCameraResolution();
        camera.setParameters(parameters);
        camera.setPreviewDisplay(previewHolder);
        startPreview();
        camera.takePicture(null, null, photoCallback);    //THIS IS WHERE THE PHOTO IS BEING TAKEN
    } catch (Exception e) {
        L.m(e.toString());
    }
}

And it errors out after that, specifically saying: java.lang.RuntimeException: takePicture failed

Just to reiterate, it takes photos just fine when I click an icon/ button to do so, what I am trying to accomplish is for it to run that code WITHOUT the need for the button/ icon to be hit. I figure it is something very simple I am missing, I just can't seem to figure it out.

Upvotes: 1

Views: 334

Answers (1)

ytRino
ytRino

Reputation: 1459

Call testing() at SurfaceHolder.Callback#onSurfaceCreated.
:-)

Upvotes: 1

Related Questions