For Kotlin :
fun getFolderName() {
val uri: Uri
val cursor: Cursor?
uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
val projection = arrayOf(MediaStore.Audio.AudioColumns.DATA)
cursor = requireActivity().contentResolver.query(uri, projection, null, null, null)
if (cursor != null) {
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Audio.AudioColumns.DATA)
}
while (cursor!!.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data)
val fileName: String = File(absolutePathOfImage).parentFile.name
}
}