【问题标题】:Android- How can I save a full photo with a custom name?Android-如何使用自定义名称保存完整照片?
【发布时间】:2019-10-12 06:40:04
【问题描述】:

我正在根据文档保存一张全尺寸照片。 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 为什么我最后会得到这么长的数字。我怎样才能摆脱它?

【问题讨论】:

    标签: android android-camera


    【解决方案1】:

    你可以试试这个

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

    【讨论】:

    • 它可以工作,但它以第二个字符“PEG_20191012_”开头
    • 请检查是否添加了正确的文件名
    • 请接受答案并点赞。快乐编码:)
    【解决方案2】:

    试试这个方法,它不仅可以通过文件名工作,还可以通过捕获图像的日期和时间来工作。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-12-29
      • 2012-10-11
      • 2017-09-20
      • 1970-01-01
      • 2021-12-09
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      相关资源
      最近更新 更多