|
不希望生成两张照片,我的代码如下
public static String takePhoto(Activity activity) { //照相
//调用相机
Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//用来打开相机的Intent
if(takePhotoIntent.resolveActivity(activity.getPackageManager())!=null){
//File externalFilesDir = activity.getExternalFilesDir(null);
File externalFilesDir = Environment.getExternalStorageDirectory();
// 获取当前日期(例如:2023-10-07)
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
String currentDate = sdf.format(new Date());
String PIC_DIR = externalFilesDir+"/Documents/Pics/"+currentDate+"/";
File file=new File(PIC_DIR);
if(!file.exists())file.mkdir();
// 保存路径
file = getFile(PIC_DIR, generateGUIDFileName());
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//设置7.0以上共享文件,分享路径定义在xml/file_paths.xml
uri = FileProvider.getUriForFile(activity, "com.tools.sealassist.fileprovider", file);
} else {
// 7.0以下,共享文件
uri = Uri.fromFile(file); // 将路径转换为Uri对象
}
takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // 表示录制完后保存的录制,如果不写,则会保存到默认的路径,在onActivityResult()的回调,通过intent.getData中返回保存的路径
//Constant.REQ_CODE为任意整数,用于回调
activity.startActivityForResult(takePhotoIntent, Constant.REQ_CODE); // 跳转
return file.getAbsolutePath();
}
return null;
} |
|