How to get name of directory having audio files using mediastore.audio in android

岁酱吖の 提交于 2021-01-29 11:08:37

问题


I'm working on audio and video player. I got all video's folder name using mediastore.video.media.bucket_display_name, it's working perfectly with video files but when i uses mediastore.audio.media.bucket_display_name it shows error. Can anybody help me to get name of folders containing audio files. I need to do this using cursor. And i got this error:

 "no such column: bucket_display_name (Sqlite code 1): , while compiling: SELECT bucket_display_name FROM audio"

回答1:


Use below code to get folders name:

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
        }
    }


来源:https://stackoverflow.com/questions/59460585/how-to-get-name-of-directory-having-audio-files-using-mediastore-audio-in-androi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!