Why can't I create a directory inside Environment.DIRECTORY_PICTURES?

This is my code.

File selfieLocation = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "Daily Selfies"); boolean isDirectory = false; if(!selfieLocation.isDirectory()) { //Creates directory named by this file selfieLocation.mkdir(); isDirectory = selfieLocation.isDirectory(); } //array of strings for(String selfiePath: selfieLocation.list()) { selfies.add(selfiePath); } 

Basically, I am trying to create my own custom directory inside a standard directory where I can place images available to the user.

I looked at related topics and saw this one, Android: it was not possible to create a directory in the default folder with pictures . However, I made sure that I had a call to getExternal .... and not just the Environment.DIRECTORY_PICTURES parameter as the parameter. I also looked here http://developer.android.com/guide/topics/data/data-storage.html#filesExternal and saw that I have the correct call method / format to create a custom folder in external memory. Example docs:

 File file = new File(Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_PICTURES), albumName); if (!file.mkdirs()) { Log.e(LOG_TAG, "Directory not created"); } 

I went through my code and saw that the local variable isDirectory remained false even after calling selfieLocation.mkdir (). Does anyone know why this directory cannot be created?

+5
source share
1 answer

Try creating a directory with File#mkdirs() rather than File#mkdir() . The latter assumes that all parent directories are already created, so it will not create the directory if its parent does not exist.
Also, take a look at your permissions in AndroidManifest.xml . The following permissions are required to read / write content to external storage:

 <manifest ...> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> ... </manifest> 

android.permission.READ_EXTERNAL_STORAGE is not yet required, but it will be in future versions of Android.

+4
source

Source: https://habr.com/ru/post/1207784/


All Articles