How to change Metadata in MediaStore Audio files / contentResolver.update always returns 0 updated


I'm attempting to change the Metadata for some (non-app owned) audio files in the MediaStore.

I've attempted to follow guidelines from:

<https://developer.android.com/training/data-storage/shared/media#manage-groups-files>

<https://developer.android.com/training/data-storage/shared/media#update-item>

<https://developer.android.com/training/data-storage/shared/media#update-other-apps-files>

This is a simplified version of the design which first requests access to the non-owned items. Then, if the user successfully grants access, attempts to update those items.

private fun requestModifyMetaGroup() {
    val urisToModify: Collection&lt;Uri?&gt; = modifyMetaList()
    val canManage = MediaStore.canManageMedia(this)
    val editPendingIntent = MediaStore.createWriteRequest(contentResolver, urisToModify)
    // Launch a system prompt requesting user permission for the operation.
   ActivityCompat.startIntentSenderForResult(this,editPendingIntent.intentSender, Constants.MODIFY_GROUP_URI_REQUEST,null, 0, 0, 0, null)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    when (requestCode) {
        Constants.MODIFY_GROUP_URI_REQUEST -&gt; {
            if (resultCode == RESULT_OK) {
                doModifyMetaGroup()
            } else {
                // TODO Action Failed Acccess
            }
        }
    }
}

private fun modifyMetaList() : List&lt;Uri&gt; {
    val track1 = &quot;content://media/external/audio/media/339&quot; // Baby
    val track2 = &quot;content://media/external/audio/media/292&quot; // Bear Hug
    val uriList: List&lt;Uri&gt; = listOf(track1.toUri(), track2.toUri())
    return uriList
}

private fun doModifyMetaGroup() {
    val uristoModify = modifyMetaList()
    val selection = &quot;${MediaStore.Audio.Media._ID} = ?&quot;
    val extUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

    uristoModify.forEachIndexed { index, uri -&gt;
        var updated = -1 // Init to -1 to show when changed
        val selectionArgs = arrayOf(ContentUris.parseId(uri).toString())
        val updateDetails = ContentValues().apply {
            put(MediaStore.Audio.Media.GENRE, &quot;NewGenre&quot;)
            put(MediaStore.Audio.Media.TITLE, &quot;NewTitle&quot;)
        }
        try {
            updated = contentResolver.update(extUri, updateDetails, selection, selectionArgs)
//            updated = contentResolver.update(uri, updateDetails, selection, selectionArgs)
//            updated = contentResolver.update(uri, updateDetails, null, null)
        } catch (e: SecurityException) {
            e.printStackTrace()
            // TODO Handle SecurityException
        } catch (e: Exception) {
            e.printStackTrace()
            // TODO Handle Other Exception
        }
    }
}

The requesting of access appears to work and even shows the thumbnails of the items being updated. The problem is with the actual update. This always returns 0 updated (and nothing is changed). No exceptions are thrown.

Changing the Uri to an invalid one returns a FileNotFoundException, so I'm confident the Uris are correct.

I've tried various ways to perform the update command as shown with the commented out lines. All have the same result.

Is anyone able to confirm what is going wrong. Is this the correct method for updating metadata or is there some other process I should be following?

Note the overall app is able to access the play audio files, so has the general access permission for this.

2
Sep 1 at 9:41 AM
User AvatarStuA
#android#kotlin#metadata#mediastore

No answer found for this question yet.