0

我正在开发一个应用程序,当我单击一次按钮后每 4 秒自动开始捕获图像..但我收到错误..这就是我正在做的

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();

    }
    });

上面的代码给出了错误..但是当我这样做时

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

它工作正常!

这是错误日志

`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)
`

任何帮助将不胜感激!

4

1 回答 1

1

那可能是因为它必须在 UI 线程上,试试这个:

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);
                    }
            });

}
});

这将在 UI 线程上每 4 秒执行一次相同的代码。不要忘记放置某种类型的标志来阻止它在您的活动暂停时执行。

于 2013-01-07T19:45:26.250 回答