1

我无法更新音频文件的 MediaStore 列,例如歌曲的标题、专辑、艺术家,尽管我使用 MediaStore.createWriteRequest() 和用户授权请求 uri 权限。当我执行 ContentResolver.update 时, noOfRowsUpdate 为 1,但未应用任何更改。

private fun requestWritePermission(uri: Uri){
    if (VERSION.SDK_INT >= VERSION_CODES.R) {
        val uriList = mutableListOf<Uri>(uri)
        val pi = MediaStore.createWriteRequest(contentResolver, uriList)
        try {
            startIntentSenderForResult(
                pi.intentSender, REQUEST_PERM_WRITE, null, 0, 0,
                0
            )
        } catch (e: SendIntentException) {
        }
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, intentData: Intent?) {

  if(requestCode == REQUEST_PERM_WRITE && resultCode == Activity.RESULT_OK){
      updateAudioTags
  }

  super.onActivityResult(requestCode, resultCode, intentData)
}

private fun updateAudioTags() {
    val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            trackId)

    val contentValues = ContentValues()

    if (hasQ()) {
        contentValues.put(MediaStore.Audio.Media.IS_PENDING, 1)
        application.contentResolver.update(uri, contentValues, null, null)
    }

    contentValues.clear()

    if (hasQ()) contentValues.put(MediaStore.Audio.Media.IS_PENDING, 0)

    contentValues.put(MediaStore.Audio.Media.TITLE, "New title")
    contentValues.put(MediaStore.Audio.Media.ALBUM, "New album")
    contentValues.put(MediaStore.Audio.Media.ARTIST, "New artist")

    val rowsUpdated = application.contentResolver.update(uri, contentValues,
            null, null)

    Log.i(TAG, "updateAudioTags() :: no of rowsUpdated : $rowsUpdated")
}

(在谷歌问题跟踪器中提出了同样的问题 - https://issuetracker.google.com/issues/173134422

4

0 回答 0