Saaram
Saaram

Reputation: 337

Android: Capture Images automatically every 4 seconds

I am developing an application that starts capturing images automatically every 4 secs after when i click the button once.. But I am getting error.. Here is what I am doing

buttonClick.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
             new Thread(new Runnable() {
                    public void run() {

                        while(true)
                        {
                        preview.camera.takePicture(shutterCallback, rawCallback,
                                jpegCallback);
                        try {
                            Thread.sleep(4000);
                        } catch (InterruptedException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        }
                        }
                }).start();

    }
    });

The above code gives error.. But when I do this

buttonClick.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
                  preview.camera.takePicture(shutterCallback,jpegCallback);
}       
});

it works fine !

Here's the error log

`01-07 23:53:16.892: W/dalvikvm(1625): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
01-07 23:53:16.912: E/AndroidRuntime(1625): FATAL EXCEPTION: main
01-07 23:53:16.912: E/AndroidRuntime(1625): java.lang.RuntimeException: Fail to connect to camera service
01-07 23:53:16.912: E/AndroidRuntime(1625):     at android.hardware.Camera.native_setup(Native Method)
01-07 23:53:16.912: E/AndroidRuntime(1625):     at android.hardware.Camera.<init>(Camera.java:110)
01-07 23:53:16.912: E/AndroidRuntime(1625):     at android.hardware.Camera.open(Camera.java:90)
01-07 23:53:16.912: E/AndroidRuntime(1625):     at com.android.mycamera.Preview.surfaceCreated(Preview.java:35)
01-07 23:53:16.912: E/AndroidRuntime(1625):     at android.view.SurfaceView.updateWindow(SurfaceView.java:532)
01-07 23:53:16.912: E/AndroidRuntime(1625):     at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339)
01-07 23:53:16.912: E/AndroidRuntime(1625):     at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
`

Any help will be highly appreciated !

Upvotes: 0

Views: 2952

Answers (1)

Budius
Budius

Reputation: 39856

that is probably because it have to be on the UI thread, try this:

buttonClick.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        final Handler h = new Handler():
        h.post(new Runnable() {
                public void run() {
                    preview.camera.takePicture(shutterCallback, rawCallback,
                            jpegCallback);
                    h.postDelayed(this, 4000);
                    }
            });

}
});

this will execute the same code, every 4 seconds, on the UI thread. Don't forget to put some type of flag to stop it from executing whenever your activity pauses.

Upvotes: 1

Related Questions