我想制作一个每5秒钟后拍摄照片的Android应用程序。目前我正在使用的技术,使用手机的相机应用程序来捕捉照片。它需要用户捕获照片,然后按下确定,然后控件返回到Android应用程序。我发现一些代码在没有人工交互的情况下做同样的事情,但由于我是android的新手,我无法理解它们的代码,因为它们大部分是不完整的并且分为多个活动。这只能通过一项活动完成吗?
编辑:上面提到的代码可以找到here
我建议你使用这个library(这里是documentation)而不是Android的官方Camera Api,这对初学者来说真的很难用
然后你的代码就像这样
private final Handler handler = new Handler(); //This should be declared before OnCreate
private Runnable photoRunnable; //This also
CameraView camera = findViewById(R.id.camera);
camera.addCameraListener(new CameraListener() {
public void onPictureTaken(PictureResult result) {
//Handle result here!
}
});
photoRunnable = new Runnable() {
@Override
public void run() {
try {
camera.takePicture(); //The result will be in onPictureTaken
}
catch (Exception e) {
e.printStackTrace();
//Handle Exception!
}
finally{
//also call the same runnable to call it at regular interval
handler.postDelayed(this, 10*1000); //10*1000 is your interval (in this case 10 seconds)
}
}
};
//runnable must be execute once
handler.post(photoRunnable);
记得要管理处理程序的生命周期