Why does Environment.getExternalStoragePublicDirectory not work on some devices (specially released since early 2011)?

I am trying to save images taken from the camera to the shared storage directory, here is my piece of code for saving the 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;
    }

works on all devices, but on some devices I get

java.io.IOException: open failed: ENOENT (There is no such file or directory)

So my question is why the same piece of code works fine on most devices and why not on some devices and how to solve this problem?

+5
source share
3 answers

Use context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)instead.

+1
source

, , , . : " , , , ?" ( ), Context.getExternalMediaDirs(). API 19 , !

" " (O_o), Environment.getExternalStorageDirectory() Environment.getExternalStoragePublicDirectory(String). . , , , . API , paths.mkdirs() , .

+1

File storageDir = new File(PATH_TO_PICTURES)

File storageDir =new File(PATH_TO_PICURES_EXTERNAL)

, , :

if(storageDir.isDirectory())
0

All Articles