How to update metadata of audio file in Android Q media store?

后端 未结 3 517
夕颜
夕颜 2020-12-09 22:52

Updating metadata of audio file in media store is not working in Android Q OS, it works in all other OS.

I am using content provider with uri specified as MediaStore

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-09 23:26

    Finally, it took some time but I figured that out.

    First, you need to obtain access to file. Here you can read about that

    Next, I found out that to update title or artist fields (maybe others to, I didn't test them) you need to set column MediaStore.Audio.Media.IS_PENDING value to 1. Like that:

        val id = //Your audio file id
    
        val values = ContentValues()
        values.put(MediaStore.Audio.Media.IS_PENDING, 1)
    
        val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)
        contentResolver.update(uri, values, null, null)
    

    And then you can edit fields that you need. Also to end the update process set MediaStore.Audio.Media.IS_PENDING to 0 again:

        val id = //Your audio file id
        val title = //New title
        val artist = //New artist
    
        val values = ContentValues()
        values.put(MediaStore.Audio.Media.IS_PENDING, 0)
        values.put(MediaStore.Audio.Media.TITLE, title)
        values.put(MediaStore.Audio.Media.ARTIST, artist)
    
        val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)
        contentResolver.update(uri, values, null, null)
    

    So in one function, it would look like this:

        @RequiresApi(value = android.os.Build.VERSION_CODES.Q)
        fun updateMetadata(contentResolver: ContentResolver, id: Long, title: String, artist: String) {
            val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id)
            val values = ContentValues()
    
            values.put(MediaStore.Audio.Media.IS_PENDING, 1)
            contentResolver.update(uri, values, null, null)
    
            values.clear()
            values.put(MediaStore.Audio.Media.IS_PENDING, 0)
            values.put(MediaStore.Audio.Media.TITLE, title)
            values.put(MediaStore.Audio.Media.ARTIST, artist)
            contentResolver.update(uri, values, null, null)
        }
    

    It's written in Kotlin but I think you will figure out how to do that in java.

    UPD

    By updating MediaStore you don't updating real file at any android version. That means, if a file would be updated (for example: renamed) and/or scanned by MediaScannerConnection your changes will be lost. This answer is right.

提交回复
热议问题