Android - How To Take A Picture Based On A Timer
I want to implement a service where, specified a time of the day, the phone will take a picture automatically. Since now, I followed the official tutorial: public class PhotoHandl
Solution 1:
Use this tutorial to trigger the camera to take a picture whenever you want.
Manifest:
<uses-sdkandroid:minSdkVersion="15" /><uses-permissionandroid:name="android.permission.CAMERA"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><applicationandroid:icon="@drawable/ic_launcher"android:label="@string/app_name" ><activityandroid:name="de.vogella.camera.api.MakePhotoActivity"android:label="@string/app_name" ><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
Layout:
<Button
android:id="@+id/captureFront"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:onClick="onClick"
android:text="Make Photo" />
To save to SD card:
publicclassPhotoHandlerimplementsPictureCallback {
privatefinal Context context;
publicPhotoHandler(Context context) {
this.context = context;
}
@OverridepublicvoidonPictureTaken(byte[] data, Camera camera) {
FilepictureFileDir= getDir();
if (!pictureFileDir.exists() && !pictureFileDir.mkdirs()) {
Log.d(MakePhotoActivity.DEBUG_TAG, "Can't create directory to save image.");
Toast.makeText(context, "Can't create directory to save image.",
Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormatdateFormat=newSimpleDateFormat("yyyymmddhhmmss");
Stringdate= dateFormat.format(newDate());
StringphotoFile="Picture_" + date + ".jpg";
Stringfilename= pictureFileDir.getPath() + File.separator + photoFile;
FilepictureFile=newFile(filename);
try {
FileOutputStreamfos=newFileOutputStream(pictureFile);
fos.write(data);
fos.close();
Toast.makeText(context, "New Image saved:" + photoFile,
Toast.LENGTH_LONG).show();
} catch (Exception error) {
Log.d(MakePhotoActivity.DEBUG_TAG, "File" + filename + "not saved: "
+ error.getMessage());
Toast.makeText(context, "Image could not be saved.",
Toast.LENGTH_LONG).show();
}
}
private File getDir() {
FilesdDir= Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
returnnewFile(sdDir, "CameraAPIDemo");
}
}
To take a photo
publicclassMakePhotoActivityextendsActivity {
privatefinalstaticStringDEBUG_TAG="MakePhotoActivity";
private Camera camera;
privateintcameraId=0;
@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// do we have a camera?if (!getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Toast.makeText(this, "No camera on this device", Toast.LENGTH_LONG)
.show();
} else {
cameraId = findFrontFacingCamera();
if (cameraId < 0) {
Toast.makeText(this, "No front facing camera found.",
Toast.LENGTH_LONG).show();
} else {
camera = Camera.open(cameraId);
}
}
}
publicvoidonClick(View view) {
camera.takePicture(null, null,
newPhotoHandler(getApplicationContext()));
}
privateintfindFrontFacingCamera() {
intcameraId= -1;
// Search for the front facing cameraintnumberOfCameras= Camera.getNumberOfCameras();
for (inti=0; i < numberOfCameras; i++) {
CameraInfoinfo=newCameraInfo();
Camera.getCameraInfo(i, info);
if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
Log.d(DEBUG_TAG, "Camera found");
cameraId = i;
break;
}
}
return cameraId;
}
@OverrideprotectedvoidonPause() {
if (camera != null) {
camera.release();
camera = null;
}
super.onPause();
}
}
EDIT: cameraId is not always set to -1, it is just a placeholder while you are looking for the front facing camera. When it is found, cameraId is set to the index of that camera
Post a Comment for "Android - How To Take A Picture Based On A Timer"