我只需要执行n次函数然后停止

时间:2017-08-18 06:41:23

标签: java android

在下面的代码中,我希望拍摄照片执行n次然后停止。

我尝试使用for循环,但收到错误java.lang.IllegalStateException: Session has been closed; further changes are illegal

protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");

            startBackgroundThread();
            if (textureView.isAvailable()) {
                openCamera();
            } else {
                textureView.setSurfaceTextureListener(textureListener);
            }


            final Timer timer = new Timer();
            timer.schedule(new TimerTask() {

                public void run() {
                    takePicture();


                }


            }, 200, 15000);


    }

1 个答案:

答案 0 :(得分:0)

您可以使用拍摄的照片数量,然后取消定时器,如:

    protected void onResume() {
        super.onResume();
        Log.e(TAG, "onResume");

            startBackgroundThread();
            if (textureView.isAvailable()) {
                openCamera();
            } else {
                textureView.setSurfaceTextureListener(textureListener);
            }

            final int PICTURES_LIMIT = 10;

            final Timer timer = new Timer();
            timer.schedule(new TimerTask() {
                int pictureNo = 0;
                public void run() {                
                    takePicture();
                    pictureNo++;
                    if (pictureNo>PICTURES_LIMIT) {
                       timer.cancel();
                    }

                }


            }, 200, 15000);


    }