-3

My question is about a code that makes pictures every "x" seconds and save them in the SD card. I don't want to make pictures forever so there's a button in the app that it should stop making pictures. The thing is I have no idea how to make it stop, shall I stop the timer or there's any other option to stop it?

Thanks in advance!

I attached the code I'm working with:

public class MainActivity extends Activity {
    private Camera mCamera;
    private Preview mCameraPreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mCamera = getCameraInstance();
        mCameraPreview = new Preview(this, mCamera);
        FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
        preview.addView(mCameraPreview);

        Button captureButton = (Button) findViewById(R.id.button_capture);
        Button pauseButton = (Button) findViewById(R.id.button_pause);

        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        mCamera.startPreview();
                        mCamera.takePicture(null,null,mPicture);
                    }
                }, 0, 2000);
            }
        });
    }

    private Camera getCameraInstance() {
        Camera camera = null;
        try {
            camera = Camera.open();
        } catch (Exception e) {
            // cannot get camera or does not exist
        }
        return camera;
    }

    PictureCallback mPicture = new PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {
            File pictureFile = getOutputMediaFile();
            if (pictureFile == null) {
                return;
            }
            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
            } catch (FileNotFoundException e) {

            } catch (IOException e) {
            }
        }

    };

    private static File getOutputMediaFile() {
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                "FOTOS");
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d("FOTOS", "failed to create directory");
                return null;
            }
        }
        // Create a media file name
        String timeStamp = new SimpleDateFormat("HHmmss_ddMMyyyy")
                .format(new Date());
        File mediaFile;
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");

        return mediaFile;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
ricriba
  • 11
  • 4

2 Answers2

0

Store the timer you create in your onClick in a class level variable. THen when the user clicks the stop button, cancel the timer with timer.cancel().

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
0

Here is the answer:

public class MainActivity extends Activity {
    private Camera mCamera = null;
    private Preview mCameraPreview = null;
    private Timer mTimer = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       mCamera = getCameraInstance();
       mCameraPreview = new Preview(this, mCamera);
       FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
       preview.addView(mCameraPreview);

       Button captureButton = (Button) findViewById(R.id.button_capture);
       Button pauseButton = (Button) findViewById(R.id.button_pause);

       captureButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
             if (mTimer != null) mTimer.cancel();
             mTimer = new Timer();
             mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mCamera.startPreview();
                    mCamera.takePicture(null,null,mPicture);
                }
             }, 0, 2000);
          }
       });
       pauseButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            if (mTimer != null) mTimer.cancel();
          }
       });
    }

    private Camera getCameraInstance() {
       Camera camera = null;
       try {
          camera = Camera.open();
       } catch (Exception e) {
          // cannot get camera or does not exist
       }
       return camera;
    }

    PictureCallback mPicture = new PictureCallback() {
       @Override
       public void onPictureTaken(byte[] data, Camera camera) {
          File pictureFile = getOutputMediaFile();
          if (pictureFile == null) {
             return;
          }
          try {
             FileOutputStream fos = new FileOutputStream(pictureFile);
             fos.write(data);
             fos.close();
          } catch (FileNotFoundException e) {

          } catch (IOException e) {
          }
       }

    };

    private static File getOutputMediaFile() {
       File mediaStorageDir = new File(
             Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
             "FOTOS");
       if (!mediaStorageDir.exists()) {
          if (!mediaStorageDir.mkdirs()) {
             Log.d("FOTOS", "failed to create directory");
             return null;
          }
       }
       // Create a media file name
       String timeStamp = new SimpleDateFormat("HHmmss_ddMMyyyy")
             .format(new Date());
       File mediaFile;
       mediaFile = new File(mediaStorageDir.getPath() + File.separator
             + "IMG_" + timeStamp + ".jpg");

       return mediaFile;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.main, menu);
       return true;
    }

}
Manitoba
  • 8,522
  • 11
  • 60
  • 122