Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Environment.getExternalStoragePublicDirectory is not working in some devices(specially manufactured since early 2011)?

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?

like image 484
alka aswal Avatar asked Mar 12 '15 08:03

alka aswal


People also ask

What can I use instead of getExternalStoragePublicDirectory?

For api 28 and below, you need getExternalStoragePublicDirectory() method but it is deprecated. What if you don't want to use that deprecated method? Then you can use SAF file explorer(Intent#ACTION_OPEN_DOCUMENT).

What is getExternalStorageDirectory in android?

getExternalStorageDirectory() : return the primary external storage root directory. Context. getExternalFilesDir(String type) : return the absolute path of the directory on the primary external storage where the application can place its own files.


1 Answers

Use context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) instead.

like image 82
Vytautas Berankis Avatar answered Sep 28 '22 23:09

Vytautas Berankis