Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
120 views
in Technique[技术] by (71.8m points)

java - How to retrieve list of all local audio files and its parameters properly?

For now, I'm doing this with content resolver. Code:

private fun loadMedia(): List<LocalMusic> {
    val list = mutableListOf<LocalMusic>()

    val contentResolver = context.contentResolver

    val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
    val selection = "${MediaStore.Audio.Media.IS_MUSIC}!= 0"
    val sortOrder = "${MediaStore.Audio.Media.TITLE} ASC"
    var i = 0
    contentResolver.query(uri, null, selection, null, sortOrder).use {
        if (it != null && it.count > 0) {
            while (it.moveToNext()) {
                val title = it.getString(it.getColumnIndex(MediaStore.MediaColumns.TITLE))
                // Android Studio shows a lint, which tells that MediaStore.MediaColumns.ARTIST 
                //requires API 30. But in Android Pie it works.
                val artist = it.getString(it.getColumnIndex(MediaStore.MediaColumns.ARTIST))
                //the same lint as described above(required api 29)
                val album = it.getString(it.getColumnIndex(MediaStore.MediaColumns.ALBUM))
                // This one is deprecated(fantastic, what to do?).
                val source = it.getString(it.getColumnIndex(MediaStore.MediaColumns.DATA))
                val duration = it.getLong(it.getColumnIndex(MediaStore.MediaColumns.DURATION))
                list.add(LocalMusic(id = "$i", artist = artist, title = title, source = source,
                        album = album, duration = duration, image = "some uri"))
                i++
            }
        }
    }
    return list.toList()
}

So, there are some questions:

  1. The main one. Should I use MediaStore.MediaColumns.DATA to get URI anyway? I know that Google tells "sometimes you don't have permissions to work with storage, blah-blah, that's why it's deprecated", but is there a better way(I always have needed permissions)?
  2. About "MediaStore.MediaColumns.ARTIST" and "MediaStore.MediaColumns.ALBUM". Why it requires API 30, if I found its usages in tutorials 5-6 years ago, though there was no API 30 at all? Because of support libraries, migrations, etc? Should I keep using it?
question from:https://stackoverflow.com/questions/65922097/how-to-retrieve-list-of-all-local-audio-files-and-its-parameters-properly

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...