Read that Android 11 has scoped storage, but I can't find any information, how can I create and use folder in /storage/emulated/0/
? Old methods works only on api 29 and below :(

- 101
- 1
- 1
- 8
-
1You can create a folder in /storage/emulated/0/Documents and other known folders. You create them in the same way as allways. – blackapps Sep 22 '20 at 19:46
-
That works, thank you so much! – Fedor Sedov Sep 22 '20 at 19:52
-
Thanks @blackapps but it must be impossible to create folder in root i meand not in document? – Amir Jan 18 '22 at 21:40
4 Answers
Since Android Q we can create folders in app specif storage. its path:
Android->data->package name->files->your folder
Use this:
File file;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
file = new File (this.getExternalFilesDir(null) + path);
} else {
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
}
if (!file.exists()) {
file.mkdirs();
}

- 8,413
- 7
- 27
- 35

- 338
- 3
- 9
-
your code works for me. Thanks. One thing : what is the cause of "null" ? – Noor Hossain Feb 17 '21 at 16:14
-
is type - direcory_music, picture etc. read here https://developer.android.com/reference/android/content/Context#getExternalFilesDir(java.lang.String) – dimvolk Feb 18 '21 at 17:56
On Android 11 the restrictions in Android 10 concerning access to external storage are much less.
Environment.getExternalStorageDirectory()
is readable again and
Environment.getExternalStoragePublicDirectory(...)
is writable for folders like Environment.DIRECTORY_DOCUMENTS
and so on.
The Android OS is very picky using the right extensions for your files in most of those directories.

- 8,011
- 2
- 11
- 25
Just if someone needs this piece of code, I am expanding the accepted answer:
Please note here I am creating two files, the first one is for the Directory where file is going to be written, second is for the file itself. You must check if the directory exists and create it, otherwise FileNotFoundException will be thrown.
File videoFile;
File videoDir;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
//Android 11+
videoDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), File.separator + "YOUR_DIRECTORY_NAME");
videoFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "/YOUR_DIRECTORY_NAME/" + videoName);
} else {
//Old Android version
videoDir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_MOVIES + File.separator + "YOUR_DIRECTORY_NAME");
videoFile = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_MOVIES + File.separator + "YOUR_DIRECTORY_NAME" + File.separator + videoName);
}
if(!videoDir.exists()) {
if(!videoDir.mkdir()) {
infoSB("No Storage Permission");
return;
}
}
try {
if(!videoFile.exists()) {
FileOutputStream fos = new FileOutputStream(videoFile);
fos.write(response);
fos.close();
}
} catch (IOException e) {}

- 726
- 1
- 11
- 29
Step 1:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Step 2:
Environment.getExternalStorageDirectory().absolutePath + "/your_folder_name"

- 63
- 6