Image capture in Android automatically

2019-04-16 06:49发布

问题:

Is there any way to use the following code to take pictures automatically i.e. no button clicks at all. Just after sometime image can be taken automatically and stored on the SD card.

protected void startCameraActivity() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(file_name)));
    startActivityForResult(intent, 1);
    finish();
}

回答1:

No, once you start an Intent you really have no control over the Activity you start (assuming it's not one that you wrote yourself). In your case, you have to make your own Activity and use the Camera API.

Check out this tutorial:

http://marakana.com/forums/android/examples/39.html



回答2:

You can use Timer & TimerTask Class together for your requirement. Just study the following code and modify it according to your usage.

import java.util.Timer;
import java.util.TimerTask;

class MyTimerTask extends TimerTask 
{
  public void run() 
  {
      // Put your camera capturing and photo saving code here
  }
}

public class MainClass 
{
  public static void main(String args[]) 
  {
    MyTimerTask myTask = new MyTimerTask();
    Timer myTimer = new Timer();

    /*
     * Set an initial delay of 15 second, then repeat every 10 second.
     */

    myTimer.schedule(myTask, 15000, 1000);
  }
}