拍摄并保存Android中的连续图像

问题描述:

这是我的代码它存储捕获的图片窗体相机并将其保存在SDcard但现在我想要增强此代码每隔5秒拍摄一次图片如果任何身体有一个想法如何请这样做,请分享拍摄并保存Android中的连续图像

public class MainActivity extends ActionBarActivity { 


    private final int requestCode = 20; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     int pictureCounter = 10; 

     imageHolder = (ImageView)findViewById(R.id.captured_photo); 
     Button capturedImageButton = (Button)findViewById(R.id.photo_button); 
     capturedImageButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       startActivityForResult(photoCaptureIntent, requestCode); 
      } 
     }); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     if(this.requestCode == requestCode && resultCode == RESULT_OK){ 
      Bitmap bitmap = (Bitmap)data.getExtras().get("data"); 

      String partFilename = currentDateFormat(); 
      storeCameraPhotoInSDCard(bitmap, partFilename); 


     } 
    } 

    private String currentDateFormat(){ 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_HH_mm_ss"); 
     String currentTimeStamp = dateFormat.format(new Date()); 
     return currentTimeStamp; 
    } 

    private void storeCameraPhotoInSDCard(Bitmap bitmap, String currentDate){ 
     File outputFile = new File(Environment.getExternalStorageDirectory(), "photo_" + currentDate + ".jpg"); 
     try { 
      FileOutputStream fileOutputStream = new FileOutputStream(outputFile); 
      bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream); 
      fileOutputStream.flush(); 
      fileOutputStream.close(); 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

您应该实现一个使用线程的可运行接口。这样,您可以调用OnClick方法的函数,然后调用Thread.sleep(timeinmilliseconds),然后再次根据需要调用onClick方法中的函数。

下面是如何使用线程的例子:

http://www.wideskills.com/java-tutorial/java-threads-tutorial

+0

感谢您的答复,但如何做到这一切! :/ – rheema

+0

1.在类的顶部添加implements可运行的。 2.创建一个线程。 3.调用方法4.调用thread.sleep()5.再次调用方法 – JordanH

+0

在我提供的链接中有清楚​​的说明。 – JordanH