Android- 如何使用自定义名称保存完整照片?

Android- How can I save a full photo with a custom name?

我正在根据文档保存一张全尺寸照片。 https://developer.android.com/training/camera/photobasics#TaskPath

private File createImageFile() throws IOException {
    // Create an image file name
    File storageDir = getExternalFilesDir("TestImageCapture");
    String timeStamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );
    String currentPhotoPath = image.getAbsolutePath();
    return image;
}

根据我的代码,它应该保存名为:JPEG_20191012_.jpg 的图像,但它保存 JPEG_20191012_200766502860978687.jpg 为什么我在最后得到这么长的数字。我怎样才能摆脱它?

你可以试试这个

 File file = new File(storageDir.getAbsolutePath()+ imageFileName+ ".jpg");
   try {
          file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
        }

试试这个方法,它不仅适用于文件名,还适用于拍摄图像的日期和时间。

private File createImageFile() throws IOException {
    // Create an image file name

    String timeStamp = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new 
    Date());
    String imageFileName = "Service IN:" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}