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 it's because of the createTempFile() method, but here's my code:
File createImageFile() throws IOException {
++image_id;
String timestamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
String subFolder = "";
if(pref_session_unique_gallery.equals("yes")){
if(event_name != null){
subFolder = event_name;
} else {
subFolder = timestamp;
}
} else {
subFolder = "_GEN";
}
if(event_name == null){
imageFileName = "CPB_"+timestamp+"-"+image_id;
} else {
imageFileName = "CPB_"+event_name+"_"+timestamp+"-"+image_id;
}
imageStorageDirectory = Environment.getExternalStorageDirectory() + File.separator + "CPB" + File.separator + subFolder;
imageFinalFileName = imageFileName;
Toast.makeText(getApplicationContext(), imageStorageDirectory + "/" + imageFileName, Toast.LENGTH_LONG).show();
File storageDirectory = new File(imageStorageDirectory);
storageDirectory.mkdir();
File image = File.createTempFile(imageFileName, ".jpg", storageDirectory);
return image;
}
When I read the toast it gives me the correct path and filename that I am expecting, but when I look in my folder view, the picture has a lot of extra numbers on it.
For example, the picture name should be CPB_20160120-1.jpg but it currently reads CPB_20160120-1484291604.jpg If it makes a difference, the file was saved at 6:37 PM
two more examples:
should be: CPB_20160120-2.jpg
is: CPB_20160120-22140921986.jpg
should be: CPB_20160120-3.jpg
is: CPB_20160120-3-965716644.jpg
Not sure where those extra numbers are coming from when the file saves...
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With