0

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?

wiki
  • 299
  • 4
  • 16
  • Where does the error occur? What is the error? – RvdK Feb 26 '13 at 14:28
  • The debugger says something similar to the file doesn't exist – wiki Feb 26 '13 at 14:48
  • 02-26 15:55:54.117: W/System.err(10701): java.io.FileNotFoundException: /mnt/extSdCard/Potos/26/1/2013.jpg: open failed: ENOENT (No such file or directory) 02-26 15:55:54.117: W/System.err(10701): at java.io.FileOutputStream.(FileOutputStream.java:88) 02-26 15:55:54.117: W/System.err(10701): at java.io.FileOutputStream.(FileOutputStream.java:128) 02-26 15:55:54.117: W/System.err(10701): at java.io.FileOutputStream.(FileOutputStream.java:117) 02-26 15:55:54.125: W/System.err(10701): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory) – wiki Feb 26 '13 at 14:57

2 Answers2

0

You should make the folder string from Environment:

String folder = Environment.getExternalStorageDirectory() + "/Photos/";

Then make sure Photos exists as a folder. Otherwise, you chould create it.

Gyonder
  • 3,674
  • 7
  • 32
  • 49
  • The function Environment.getExternalStorageDirectory() is deprecated in android 4.0.4, this is not the problem because when i read files from external sd card i use as path "/mnt/extSdCard/" and it works – wiki Feb 26 '13 at 14:45
  • Do NOT use hardcoded paths. It will break on different Android devices. Use the Environment to retrieve writable directories. – RvdK Feb 26 '13 at 15:17
  • By the way, I don't see that it's deprecated on the Android webpage: http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory() – RvdK Feb 26 '13 at 15:18
  • i say you thay this function don't works in devices that has sdcar as folder that internal memory y extSdCard as external memory, in developre can say this, but if yo read forums you read that hoy have to use hardcoded paths – wiki Feb 26 '13 at 15:34
0

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.

Eddy Talvala
  • 17,243
  • 2
  • 42
  • 47
  • Thank you very much, I will try to use the code as follows:`code` String name = day + "-" + month + "-" + year + ".jpg"; I think that it would work. I've read in many web sites that is a bad practice to use hardcode, but in samsungs devices is impossible use evirotment or at least I have not found how to get it. – wiki Mar 01 '13 at 01:04