在Android 11中,bulkInsert解析器似乎没有在音频MediaStore中工作。为了解决这个问题,您可以使用其他方法将音频文件插入MediaStore。
下面是一个带有代码示例的解决方案,可以将音频文件添加到MediaStore:
val resolver = context.contentResolver
val contentValues = ContentValues().apply {
put(MediaStore.Audio.Media.DISPLAY_NAME, fileName)
put(MediaStore.Audio.Media.TITLE, songTitle)
put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg")
put(MediaStore.Audio.Media.RELATIVE_PATH, "Music/$albumName")
put(MediaStore.Audio.Media.IS_PENDING, 1) // Add this to mark file as pending
}
// Insert the new audio file into MediaStore.
val newUri = resolver.insert(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, contentValues)
newUri?.let {
resolver.openFileDescriptor(newUri, "w", null)?.use { descriptor ->
val outputStream = FileOutputStream(descriptor.fileDescriptor)
outputStream.write(fileContent)
outputStream.close()
}
contentValues.clear()
contentValues.put(MediaStore.Audio.Media.IS_PENDING, 0) // Update this to mark file as not pending
resolver.update(newUri, contentValues, null, null)
}
这段代码使用ContentValues对象为音频文件设置属性,并使用insert()方法将文件插入到MediaStore中。然后,使用openFileDescriptor()方法和FileOutputStream类将文件内容写入新文件中。最后,使用update()方法将文件标记为“非待处理文件”。
使用此代码,您应该可以成功将音频文件插入到MediaStore中。