I have searched for a while but was unable to find any possible way to create a folder in Android Q or higher. Since the methods getExternalStorageDirectory() and getExternalStoragePublicDirectory() are deprecated. I can not find any possible way to create a directory in Internal Storage. I have tried Can't create directory in Android 10 , File operations in android Q beta, how to grant full Write/Read files access on specifics folder in android Q, and many more.
Asked
Active
Viewed 630 times
1
Zain
- 25,701
- 7
- 36
- 61
Ashutosh Sharma
- 53
- 8
3 Answers
2
if your want to create a directory in a pre-defined folder (e.g. "Environment.DIRECTORY_PICTURE"), you can use the following codes.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val folderName = "TESTING"
val contentValues = ContentValues().apply {
put(MediaStore.MediaColumns.DISPLAY_NAME, folderName)
put( MediaStore.MediaColumns.RELATIVE_PATH,
"${Environment.DIRECTORY_PICTURES}/$folderName" )
}
//the following line will create the folder "TESTING" within
//"Pictures" directory.
contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)
}
do note that "RELATIVE_PATH" is only available on API > 29 hence the version check.
Angel Koh
- 11,377
- 7
- 59
- 84
1
you can add in manifest like below
<application android:requestLegacyExternalStorage="true">
Mert KARADENIZ
- 71
- 5
0
Use getExternalFilesDir(String)
Noah
- 369
- 3
- 16
-
But that can only store in my app's directory! I want to store in Internal Storage upper level directory! – Ashutosh Sharma Jun 13 '20 at 03:52
-
Well that is not possible anymore in a normal way. But there is a temporary solution for Q. Read the posts here as your problem has been discussed here nearly every day for the last year. – blackapps Jun 13 '20 at 07:55