Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android中捕获并保存连续图像_Android - Fatal编程技术网

在Android中捕获并保存连续图像

在Android中捕获并保存连续图像,android,Android,这是我的代码,它存储从相机捕获的图片并将其保存在SD卡上,但现在我想将此代码增强为每5秒拍摄一次,如果有人知道如何执行此操作,请与我们分享 public class MainActivity extends ActionBarActivity { private final int requestCode = 20; @Override protected void onCreate(Bundle savedInstanceState) { supe

这是我的代码,它存储从相机捕获的图片并将其保存在SD卡上,但现在我想将此代码增强为每5秒拍摄一次,如果有人知道如何执行此操作,请与我们分享

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(timeinmillizes),然后根据需要在OnClick方法内再次调用函数

下面是一个如何使用线程的示例:


1。在类的顶部添加可运行的实现。2.创建一个线程。3.call方法4.call thread.sleep()5。再打电话给我,请给我发电子邮件!