Ashutosh Sharma
Ashutosh Sharma

Reputation: 83

How to create a directory in Android Q and higher

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.

Upvotes: 1

Views: 1197

Answers (3)

Angel Koh
Angel Koh

Reputation: 13495

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.

Upvotes: 2

Mert KARADENIZ
Mert KARADENIZ

Reputation: 71

you can add in manifest like below

<application android:requestLegacyExternalStorage="true">

Upvotes: 1

Noah
Noah

Reputation: 701

Use getExternalFilesDir(String)

Upvotes: 0

Related Questions