Search code examples
androidandroid-cameraandroid-photos

Naming a photo (date)


I am developing an app for andorid where I take pictures. The problem is that I only save cone l conseguidor current time in milliseconds and I want to put the current date (dd / mm / yyyy).

This is my code:

PictureCallback jpegCallback = new PictureCallback() 
{
    public void onPictureTaken(byte[] data, Camera camera)
    {
    FileOutputStream outStream = null;
    try
        {               
    outStream = new FileOutputStream(String.format("/mnt/extSdCard/Photos/%d.jpg", System.currentTimeMillis()));    
    outStream.write(data);
    outStream.close();

    } catch (FileNotFoundException e) {
            e.printStackTrace();
    } catch (IOException e) {
            e.printStackTrace();
    } finally {
        }

}
};

I tried to put this, but does not work I get error:

Calendar c = Calendar.getInstance();
String day = Integer.toString(c.get(Calendar.DATE));
String month = Integer.toString(c.get(Calendar.MONTH));
String year = Integer.toString(c.get(Calendar.YEAR));

String name = day + "/" + month + "/" + year + ".jpg";
String folder = "/mnt/extSdCard/Photos/";
String path = folder + name;

...

outStream = new FileOutputStream(String.format(path));  
outStream.write(data);
outStream.close();

Can anyone help mep?


Solution

  • You cannot use '/' in filenames. That's interpreted by the system as a set of directories, and unless you've created all the directories ahead of time, you'll get this error. For example, if it's January 1, 2014, your filename will be: /mnt/extSdCard/Photos/01/01/2014.jpg.

    You need to either create the 01/01 directory, or change your separator to be something else, like '_' or '-'.

    In addition, do not hardcode /mnt/extSdCard without at least a whitelist of devices to use it on. It does not exist on many, many devices, including all Nexus devices.