如何在Android Q媒体商店中更新音频文件的元数据? [英] How to update metadata of audio file in Android Q media store?
问题描述
在Android Q OS中无法更新媒体存储中音频文件的元数据,在所有其他OS中都可以使用
Updating metadata of audio file in media store is not working in Android Q OS, it works in all other OS.
我正在使用内容提供商,并将uri指定为MediaStore.Audio.Media.EXTERNAL_CONTENT_URI.在所有Android Q设备以下都可以正常工作.下面是我用来更新跟踪元数据的代码.
I am using content provider with uri specified as MediaStore.Audio.Media.EXTERNAL_CONTENT_URI. It is working fine in all below Android Q device. Below is the code that I am using to update track metadata.
ContentValues cv = new ContentValues();
ContentResolver resolver = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
cv.put(MediaStore.Audio.Media.TITLE, newTitle);
cv.put(MediaStore.Audio.Media.ALBUM, newAlbumName);
cv.put(MediaStore.Audio.Media.ARTIST, newArtistName);
int rowsUpdated = resolver.update(uri, cv,
MediaStore.Audio.Media._ID + " = ? ", new String[]{audioId});
对于Android Q设备,rowsUpdated始终为0,无一例外. 其他音乐播放器如何更新Android Q中的曲目元数据?
For Android Q device, rowsUpdated is always 0 with no exception. How are other music player updating tracks metadata in Android Q ?
推荐答案
最后,花了一些时间,但我发现了.
Finally, it took some time but I figured that out.
首先,您需要访问文件. 在这里您可以阅读有关
First, you need to obtain access to file. Here you can read about that
接下来,我发现要更新标题或艺术家字段(也许其他人没有对它们进行测试),则需要将列MediaStore.Audio.Media.IS_PENDING
的值设置为1.这样:
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)
然后您可以编辑所需的字段.另外,要结束更新过程,请将MediaStore.Audio.Media.IS_PENDING
再次设置为0:
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)
}
它是用Kotlin写的,但我想您会想出如何用Java做到这一点的.
It's written in Kotlin but I think you will figure out how to do that in java.
UPD
通过更新MediaStore
,您不会在任何android版本上更新实际文件.这意味着,如果文件被MediaScannerConnection
更新(例如:重命名)和/或扫描,您的更改将丢失. 此答案是正确的.
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.
这篇关于如何在Android Q媒体商店中更新音频文件的元数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!