Files that a user or other applications may need to access later on should be stored in a shared public directory. Examples of such directories are the Pictures/ and Music/ directories.
The getExternalStoragePulicDirectory() method should be called by your application in order to retrieve a File representing a required public directory. The type of directory to be retrieved should be passed as the sole argument to the function.
The following is a function that creates a directory for music to be stored:
private fun getMusicStorageDir(collectionName: String): File {
val file = File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC), collectionName)
if (!file.mkdir()) {
Log.d("DIR_CREATION_STATUS", "Directory creation failed.")
}
return file
}
In the case where an error occurs when creating the public directory, an appropriate message is logged to the console.