Reputation: 1625
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)
Upvotes: 1
Views: 4490
Reputation: 1
After a bit of research on .mp3 files, I found out that the ContentProvider itself, after scanning the .mp3 files, tries to read its ID3v2 tags.
If there is no tag or there is no title frame in it, then the file name without the extension is set as the TITLE column in the MediaStore. If the tags are in place, then the corresponding fields in the MediaStore are filled.
For example, the ARTIST field is filled with the value of the TPE1 or TP1 frame (depending on the frame version), which is responsible for the artist's name. If there is one, of course.
Thus, the solution can be direct editing of the tags inside the .mp3 file using some parser. After that, force the content provider to rescan this file using MediaScannerConnection. You can find how to use MediaScannerConnection to scan updated file here: Android How to use MediaScannerConnection scanFile.
Upvotes: 0