Android Kotlin Auto Close Resource/Cursor on Exception

September 24, 2019
val contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI

val projections = arrayOf(
    MediaStore.Images.Media._ID,
    MediaStore.Images.ImageColumns.DATA
)

contentResolver.query(contentUri, projections, null, null, null)?.use { cursor ->
    if (cursor.moveToFirst()) {
        val imageUriIndex = imageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)

    }

    do {
        val imageUri = imageCursor.getString(imageUriIndex)
    } while (cursor.moveToNext())

    // cursor.close()
}

NOTE: Refer use

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.