Asserting media availability

Sometimes—for one reason or another, such as the storage device being missing—an external storage medium may not be available for access. As a consequence, it is important to check the availability of external storage media before attempting to make use of them.

The getExternalStorageState() method should be called in order to check whether media are available. You can use the following code snippet to check whether external storage is available to be written to in your application:

private fun isExternalStorageWritable(): Boolean {  val state = Environment.getExternalStorageState()  return Environment.MEDIA_MOUNTED == state}

Firstly, we retrieve the current state of external storage and then we check whether it ...

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.