1

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.

Zain
  • 37,492
  • 7
  • 60
  • 84

3 Answers3

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
  • 12,479
  • 7
  • 64
  • 91
  • I am using the same thing, it is creating folder, At the same time it is also creating and Image file entry in MediaStore, which is showing as an invalid image in device Gallary – mohitum Jul 07 '22 at 11:36
1

you can add in manifest like below

<application android:requestLegacyExternalStorage="true">
0

Use getExternalFilesDir(String)

Noah
  • 567
  • 5
  • 21
  • 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