I have a piece of code that has always worked for me, which creates a directory in "internal memory" :
String direc = Environment.getExternalStorageDirectory().toString() + "/nombrecarpeta/";
and create the folder with:
new File(direc).mkdirs();
remember that the
Environment.getExternalStorageDirectory().toString()
bring back
/storage/emulated/0
it works, but in android 10 and later it doesn't create the directory anymore.
I understand that if you add in the manifest
android:requestLegacyExternalStorage="true"
in android 10 it will work but in android 11 no longer..
the idea is not to use the
android:requestLegacyExternalStorage="true"
any ideas?
When trying to create your directory
/storage/emulated/0/nombrecarpeta/
usingyou comment "it works .. but in android 10 and later not anymore."
This was defined in the documentation, as of API 29 you will no longer have access to locations outside your application's directory, now you will have to create directories and files within the structure defined by Android:
Probably for security reasons.
As you comment you can temporarily define
android:requestLegacyExternalStorage="true"
within yourAndroidManifest.xml
for android 10 and 11, but for future OS versions this will definitely be restricted, so I recommend you now use getExternalFilesDir() for this purpose.Now you have to do it this way:
Create directory using getExternalFilesDir()
This will create the directory and structure:
Create file using getExternalFilesDir()
This will create the file "gerardfile.txt" within the structure: