Android New Image has strange numbers on the end of the file name

后端 未结 2 1122
闹比i
闹比i 2021-01-19 16:07

I\'m writing a camera2 app in android and when I try to save the image, something adds extra numbers on the end of the filename before the \'.jpg\'

I have a feeling

相关标签:
2条回答
  • 2021-01-19 16:20

    According implementation of used method new file is created with extra random integer new File(tmpDirFile, prefix + Math.randomIntInternal() + suffix)

    public static File createTempFile(String prefix, String suffix, File directory)
                throws IOException {
            // Force a prefix null check first
            if (prefix.length() < 3) {
                throw new IllegalArgumentException("prefix must be at least 3 characters");
            }
            if (suffix == null) {
                suffix = ".tmp";
            }
            File tmpDirFile = directory;
            if (tmpDirFile == null) {
                String tmpDir = System.getProperty("java.io.tmpdir", ".");
                tmpDirFile = new File(tmpDir);
            }
            File result;
            do {
                result = new File(tmpDirFile, prefix + Math.randomIntInternal() + suffix);
            } while (!result.createNewFile());
            return result;
        }
    
    0 讨论(0)
  • 2021-01-19 16:22

    Those random numbers are explicitly generated by createTempFile(), as seen in the source code.

    You probably don't want to use temporary files anyway, thus I'd recommend to create normal files:

    File image = new File(storageDirectory, imageFileName + ".jpg");
    
    0 讨论(0)
提交回复
热议问题