我正在尝试更新音频文件的流派标签。
CODE
final Uri genreUri = MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
String currentGenreName = MediaStore.Audio.Genres.NAME;
ContentValues values2 = new ContentValues();
values2.put(currentGenreName, genre);
String whereGenre = MediaStore.Audio.Genres._ID + "=?";
String[] whereVal2 = {Long.toString(genreID)};
resolver.update(genreUri, values2, whereGenre, whereVal2);
我没有收到任何错误,但是MediaStore风格表中的风格没有更新。
[其他标签,例如标题,艺术家,专辑和艺术品,已更新,仅流派不起作用。
我也找不到有关更新音频文件中流派名称的任何信息,我只能通过上面的标签来做到这一点。
编辑
我用于编辑标题,艺术家,专辑名称和年份标签的代码,它可以正常工作,所以我认为我必须对类型名称进行相同的操作,因为我具有类型ID。
final Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = c.getContentResolver();
String currentTitle = MediaStore.Audio.Media.TITLE;
String currentArtist = MediaStore.Audio.Media.ARTIST;
String currentAlbumID = MediaStore.Audio.Media.ALBUM_ID;
String currentAlbum = MediaStore.Audio.Media.ALBUM;
String currentGenreID = MediaStore.Audio.Genres._ID;
String currentGenreName = MediaStore.Audio.Genres.NAME;
String currentAlbumData = MediaStore.Audio.Media.DATA;
String currentYear = MediaStore.Audio.Media.YEAR;
ContentValues values = new ContentValues();
values.put(currentTitle, title);
values.put(currentArtist, artist);
values.put(currentAlbum, album);
values.put(currentYear, year);
//Update song info.
String where = MediaStore.Audio.Media._ID + "=?";
String[] whereVal = {Long.toString(songID)};
resolver.update(musicUri, values, where, whereVal);
为什么不更新?
您必须使用内容解析器输出流方法来覆盖Android Q中的旧元数据,而且它有点冗长... ...>
if (Build.VERSION.SDK_INT >= VERSION_CODES.Q) { Log.d(TAG, "Android Q") val displayName = getDisplayName(mSong.id) val filePath = getFile(mSong.id, displayName) alterMetadata(filePath, mSong.artist, mSong.track, mSong.album, mSong.genre, mSong.metadataYear, mSong.metadataWriter) updateViaResolver(mSong.id, filePath) } else { Log.d(TAG, "Less then Android Q") updateMetadataPreQ(mSong.id, mSong.album, mSong.artist) }
获取原始文件名以创建临时文件
private fun getDisplayName(mId: String?): String? { val uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI val projection = arrayOf(MediaStore.Audio.Media.DISPLAY_NAME) val selection = "${MediaStore.Audio.Media._ID} =?" val selectionArgs = arrayOf(mId) val cursor = mContext.contentResolver.query(uri, projection, selection, selectionArgs, null) var displayName: String? = null cursor?.use { while (cursor.moveToNext()) { displayName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME)) } return displayName } return null }
获取文件
private fun getFile(mId: String, displayName: String?): String? { displayName?.let { val file = File(displayName) val prefix = file.nameWithoutExtension val fileExtension = file.extension val outputDir = mContext.cacheDir Log.d(TAG, "displayName $displayName prefix $prefix, fileExtension $fileExtension, outputDir $outputDir") try { val outputFile = File.createTempFile(prefix, ".$fileExtension", outputDir) val resolver: ContentResolver = mContext.contentResolver val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mId.toLong()) resolver.openInputStream(uri)?.use { stream -> outputFile.copyInputStreamToFile(stream) } return outputFile.absolutePath } catch (ex: IOException) { Log.d(TAG, "Exception ${ex.message}") } } return null }
然后我使用J Audio Tagger更改ID3标签。
以及我通过解析器方法进行的更新
// From https://developer.android.com/reference/android/content/ContentProvider // String: Access mode for the file. May be // "r" for read-only access, // "w" for write-only access (erasing whatever data is currently in the file), // "wa" for write-only access to append to any existing data, // "rw" for read and write access on any existing data, and // "rwt" for read and write access that truncates any existing file. This value must never be null. private fun updateViaResolver(mId: String, filePath: String?) { filePath?.let { val file = File(filePath) val uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mId.toLong()) mContext.application.contentResolver.openOutputStream(uri, "w")?.use { stream -> stream.write(file.readBytes()) } } }
助手方法
private fun File.copyInputStreamToFile(inputStream: InputStream?) { this.outputStream().use { fileOut -> inputStream?.copyTo(fileOut) } }
此文章发布于Android Q ...