Java 安卓系统中相机图像的文件名中附加了随机数?

Java 安卓系统中相机图像的文件名中附加了随机数?,java,android,Java,Android,我正在通过我的应用程序上的摄像头拍照。在将输出保存到临时文件之前,我使用一种方法创建一个临时文件 当我创建临时文件时,它的文件名是我希望它在dateformat中类似于-yyyyMMdd_hhmmss的格式 但是当我调用photoUri时,一个随机数被添加到文件中 这个数字来自哪里?我如何阻止它添加到图像文件中?额外的数字看起来像451308260449067127,每次都是随机的 @Override public void onOtherButtonClick(ActionSheet acti

我正在通过我的应用程序上的摄像头拍照。在将输出保存到临时文件之前,我使用一种方法创建一个临时文件

当我创建临时文件时,它的文件名是我希望它在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;
}
像这样做

你得到的随机数是以毫秒为单位的时间

SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss", Locale.getDefault());
String timeStamp=dateFormat.format(new Date());

使用上述代码获取时间戳。

在createImageFile()方法中使用这种方式

这就是工作原理-它接受前缀、后缀和目录,并返回一个以前不存在的
文件。在实践中,这种情况会发生


如果您不想这样做,请不要使用
createTempFile()
而是使用常规的。

使用字符串拆分函数删除结束号451308260449067127,然后您可以添加时间戳。时间戳是正确的。它确实按照我想要的方式命名文件。但它仍然会将这个19位数字添加到文件的末尾。如果您当时正在使用游标插入图像,则可能会添加时间戳。我尝试了这个方法。时间戳是正确的。但当创建临时文件时,仍会附加一些数字。
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" ;
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.US);
Date now = new Date();
String fileName = formatter.format(now);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd_hhmmss", Locale.getDefault());
String timeStamp=dateFormat.format(new Date());
  File image = File.createTempFile(
        imageFileName,  /* prefix */
       System.currentTimeMillis()+".png",         /* suffix */
        storageDir      /* directory */
);