1

I am not able to update MediaStore columns of an audio file like title, album , artist of the song, though I request for uri permission using MediaStore.createWriteRequest() and user grants. When I do ContentResolver.update , noOfRowsUpdate is 1, but no changes are applied.

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

(Have raised the issue for the same in google issue tracker - https://issuetracker.google.com/issues/173134422)

AndroidDev
  • 1,485
  • 2
  • 18
  • 33

0 Answers0