7

I am trying to store the images captured from camera to public storage directory, here is my part of code for storing image:

protected File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
                );
        return image;
    }

its working fine in most of the devices, but in some devices i get

java.io.IOException: open failed: ENOENT (No such file or directory)

So my question is why the same piece of code is working fine in most of the devices and why not in some devices and how to fix this issue?

Android007
  • 155
  • 11
alka aswal
  • 511
  • 1
  • 7
  • 22
  • see this : http://stackoverflow.com/questions/11281010/how-can-i-get-external-sd-card-path-for-android-4-0 – Milad Faridnia Mar 12 '15 at 08:13
  • yes, your provided link is very helpful, but my concern was for getting public directories, it was working fine in some devices but was not working in some, now i am checking it first whether the directory exists or not, that solved my problem..:-) thanx one again :-) – alka aswal Mar 17 '15 at 11:07
  • did you provided the necessary permissions? – karan Oct 23 '15 at 06:48

3 Answers3

3

Use context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) instead.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • thank you @Vytautas .. bt if you can elaborate a little bit about why getExternalStoragePublicDirectory is not working in my case and why this will work, it will be great. :-) – alka aswal Mar 12 '15 at 10:08
  • calling 'getExternalStoragePublicDirectory' you are not sure that directory exists, so you must check it first if it does not egzists, create one. More information you can find here: [link](http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)) – Vytautas Berankis Mar 12 '15 at 12:22
  • ok , that means, it is varying device to device , in some devices public directories exists and in some it doesn't exists, so that's why we have to check it first? – alka aswal Mar 12 '15 at 14:17
  • i think it is deprecated now for newer versions – bhavya joshi Jun 24 '19 at 12:39
  • No, it is not. Tested on android P. – Vytautas Berankis Jun 26 '19 at 08:13
  • deprecated in android Q – latifalbr May 07 '20 at 01:33
  • No, its not (https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String)). https://developer.android.com/reference/android/os/Environment#getExternalStorageDirectory() and https://developer.android.com/reference/android/os/Environment#getExternalStoragePublicDirectory(java.lang.String) are deprecated. – Vytautas Berankis May 07 '20 at 10:57
1

If you're storing your images in a public directory, I guess it is because you want to share those with other apps. You need to ask yourself the following question: "Do I want those files generated by my app to survive my app being uninstalled?" If the answer is no (which I recommend) you should use Context.getExternalMediaDirs() instead. Plus for API 19 and above, these directories do not need storage permissions!

If the answer is yes (O_o), you should use Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory(String). These always require storage permissions. Also, as you have already experienced and others pointed out, these directories might not yet exist. Check the API documentation for more info, but in short, you should call paths.mkdirs() before to ensure the dirs exist.

tfad334
  • 558
  • 6
  • 14
Aitor Viana
  • 933
  • 6
  • 15
0

you can use

File storageDir = new File(PATH_TO_PICTURES)

or

File storageDir =new File(PATH_TO_PICURES_EXTERNAL)

of course its better to check the directory first using this code:

if(storageDir.isDirectory())
Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78