Saving image to sd with current date and time in name doesn't work

android, android-camera, datetime, image, save

Solution

Colon `:` is not a valid character in a file name, that is why it is failing to create such a file. Try change your name pattern to something like this:

CharSequence s  = DateFormat.format("MM-dd-yy hh-mm-ss", d.getTime());

Problem

In my application I use a button to launch Camera application and save picture to specific folder on sdCard naming it by current date and time. When I hardcode the name for the picture, it works fine, but if I'm trying to put date in the name it doesn't work at all. ``` Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); File imagesFolder = new File(Environment.getExternalStorageDirectory(), Constants.IMAGE_FOLDER_URI); imagesFolder.mkdirs(); Date d = new Date(); CharSequence s = DateFormat.format("MM-dd-yy hh:mm:ss", d.getTime()); File image = new File(imagesFolder, s.toString() + ".jpg"); //this line doesn't work Uri uriSavedImage = Uri.fromFile(image); imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage); startActivity(imageIntent); ``` If I put: ``` s = "some_name"; ``` then it works, but I need current date and time in image name.

Original source