Reputation: 1
I'm creating New TmepFile ,and want to make format "yyyyMMdd" but it's work until createTempFile .
private File createFile() throws IOException{
String tempName = new SimpleDateFormat("yyyyMMdd").format(new Date());
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
Log.d("e",tempName);
File image = File.createTempFile(
tempName,
".png",
storageDir
);
Log.d("e",image.getName());
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
Log
D/e: 20190402
D/e: 201904021419980777854538831.png
Upvotes: 0
Views: 171
Reputation: 1336
File.createTempFile
lets you specifiy a prefix and suffix for the file created but you don't specify the full name as it has a random part in its name. If you want to have a file in the temp-directory with exactly that name, you would need to use something like this:
File image = new File(
System.getProperty("java.io.tmpdir") ,
tempName +
".png"
);
Upvotes: 1
Reputation: 86
Seems to be part of the createTempFile
method itself. I was looking at docs and it says "Once these adjustments have been made the name of the new file will be generated by concatenating the prefix, five or more internally-generated characters, and the suffix.".
It seems it generates random characters at the end to guarantee uniqueness.
What you can do I guess is store the file object somewhere you where you can access it where you need to and just get the first 6 characters which should be the date or you can just create a normal file. Also possible that maybe you can just rename the temporary file after creating it to only use first 6 characters? Not entirely sure
Upvotes: 0
Reputation: 2434
Simple answer, you can't change the name of the file created using createTempFile
. It has a default unique name for each file.
Long answer,according to the https://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile(java.lang.String,%20java.lang.String,%20java.io.File) :
Upvotes: 0