Storing sharable files

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", ...

Get Kotlin Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.