我在我的应用上通过相机拍照。我在保存输出之前使用方法创建临时文件。
当我创建临时文件时,它的文件名是我想要的样子 - 在dateformat中的yyyyMMdd_hhmmss。
但是当我调用photoUri时,会在文件中添加一个随机数。
这个号码来自何处以及如何阻止它添加到图像文件中?额外的数字看起来像这个451308260449067127,它每次都随机。
@Override
public void onOtherButtonClick(ActionSheet actionSheet, int index) {
String[] galleryPermissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (EasyPermissions.hasPermissions(this, galleryPermissions)){
//Camera
if (index == 0) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
}
catch (IOException ex) {
Toast.makeText(getApplicationContext(),ex.getMessage(),Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = null;
//
// N is for Nougat Api 24 Android 7
if (Build.VERSION_CODES.N <= android.os.Build.VERSION.SDK_INT) {
// FileProvider required for Android 7. Sending a file URI throws exception.
photoURI = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID + ".provider",
photoFile);
Log.v("File Name",photoURI.toString());
} else {
// For older devices:
// Samsung Galaxy Tab 7" 2 (Samsung GT-P3113 Android 4.2.2, API 17)
// Samsung S3
photoURI = Uri.fromFile(photoFile);
Log.v("lowbuild",photoURI.toString());
}
//Starts the intent to camera where extra output is stored as photoURI
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
Log.v("Extra",MediaStore.EXTRA_OUTPUT.toLowerCase().toString());
startActivityForResult(takePictureIntent, 0);
}
else{
// IF create file somehow returned an empty file
Log.v("file", "photo file is null");
Toast.makeText(getApplicationContext(),"Photo file is null", Toast.LENGTH_LONG).show();
}
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = SimpleDateFormat("yyyyMMdd_hhmmss", Locale.getDefault()).format(new Date());
String imageFileName = timeStamp;
Log.v("time",timeStamp);
Log.v("image",imageFileName);
File storageDir = new File( Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "Camera");
if(!storageDir.exists()){
boolean s = new File(storageDir.getPath()).mkdirs();
if(!s){
Log.v("not", "not created");
}
else{
Log.v("cr","directory created");
}
}
else{
Log.v("directory", "directory exists");
}
File image = File.createTempFile(
imageFileName, /* prefix */
".png", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
uriOfImage = Uri.parse(image.getPath());
return image;
}
编辑:没有一个答案对我有帮助。
我是这样调试的。 Log.v("image2",imageFileName);
File image = File.createTempFile(
imageFileName, /* prefix */
".png" , /* suffix */
storageDir /* directory */
);
Log.v("file",image.getAbsolutePath());
imagefilename is - 20180220_040841
But file abosolute path is - /storage/emulated/0/DCIM/Camera/20180220_0408412097619931102966329.png
答案 0 :(得分:1)
这就是File.createTempFile()
的工作方式 - 它采用您的前缀,后缀和目录,并返回之前不存在的File
。实际上是appending a large random number发生的。
如果您不想这样做,请不要使用createTempFile()
,而应使用常规File
constructors之一。
答案 1 :(得分:0)
long msTime = System.currentTimeMillis();
Date dt = new Date(msTime);
String format = "yyMMddHHmmssSSS";
SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.US);
String PhotoFileName = String.format("Photo%s", sdf.format(dt).toString());
String saveFilename = PhotoFileName + ".png" ;
答案 2 :(得分:0)
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date now = new Date();
String fileName = formatter.format(now);
这样做。
您获得的随机数是以毫秒为单位的时间。
答案 3 :(得分:0)
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss", Locale.getDefault());
String timeStamp=dateFormat.format(new Date());
使用上面的代码来获取时间戳。
答案 4 :(得分:0)
在createImageFile()方法中使用这种方法
File image = File.createTempFile(
imageFileName, /* prefix */
System.currentTimeMillis()+".png", /* suffix */
storageDir /* directory */
);