在(一)中我们主要完成了相机预览和拍照设置,本文来实现核心功能拍照
拍照
首先需要生成文件名。文件名首先标记文件类型,然后是时间,如IMG_20160503_153218.jpg
在CameraPreview中添加
public static final int MEDIA_TYPE_IMAGE = 1;
private Uri outputMediaFileUri;
private String outputMediaFileType;
private File getOutputMediaFile(int type) {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), TAG);
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d(TAG, "failed to create directory");
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_" + timeStamp + ".jpg");
outputMediaFileType = "image/*";
} else {
return null;
}
outputMediaFileUri = Uri.fromFile(mediaFile);
return mediaFile;
}
public Uri getOutputMediaFileUri() {
return outputMediaFileUri;