Android Studio 使用内容解析程序查询与介质ID列表匹配的介质列表

3j86kqsm  于 2022-11-16  发布在  Android
关注(0)|答案(1)|浏览(114)

我想使用内容解析器获取与媒体ID列表匹配的歌曲列表。
例如,G

SELECT FROM AUDIO WHERE _id = {223, 22, 38, 90}.

因此,这将返回具有相应ID的介质列表,因此我将获得介质223、介质22、介质38和介质90
我正在执行类似的操作,但它返回的是一个空列表。

private var selectionArgs = ArrayList<Long>()

private val projection = arrayOf(
    MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.ARTIST,
    MediaStore.Audio.Media.TITLE,
    MediaStore.Audio.Media.DISPLAY_NAME,
)
private fun getColumnIndex(cursor: Cursor, columnName: String): Int{
    return cursor.getColumnIndex(columnName)
}
fun getSongsInPlaylist(songIds: List<Long>): List<Songs> {
    val selection = "${MediaStore.Audio.Media._ID} = ?"
    selectionArgs = songIds as ArrayList<Long>

 val songList =  mutableListOf<Songs>()

    val collection = Utility.sdk29AndUp {
        MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL)
    } ?:  MediaStore.Audio.Media.EXTERNAL_CONTENT_URI

    //Get songs from provider
    context.contentResolver.query(
        collection,
        projection,
        selection,
        arrayOf(selectionArgs.toTypedArray().toString()),
        MediaStore.Audio.Media.DEFAULT_SORT_ORDER //Sort in alphabetical order based on 
  display name.
    ).use { cursor ->
        if (cursor?.moveToFirst() == true) {
            do {
            ..........
       }

  songList.add(
  Songs(
  mediaId = id,
  title = truncatedTitle,
  subtitle = truncatedArtisteName,                 
  )
  )
  } while (cursor.moveToNext())
        }
    }
 return songList

此代码返回{}的空列表,同时将mediaId列表传递给查询...
但是当我尝试使用单一媒体时..像这样:

val selection = "${MediaStore.Audio.Media._ID} = 38"

它的工作...
我需要一个媒体列表...与媒体相匹配

oalqel3c

oalqel3c1#

若要取得符合ID的媒介清单,您必须提供(?)字段的数目,以了解SQL数据库。
我知道这不是最好的做法,但这是完成任务。

在Kotlin

selectionArgs = songIds as ArrayList<Long>

val questionmark = mutableList<String>()

 for(element in selectionArgs){
   questionmark.add("?")   
}

val selection = "${MediaStore.Audio.Media._ID} IN (${questionmark.joinToString(",")})"
   
 context.contentResolver.query(
        collection,
        projection,
        selection,
        selectionArgs.toTypedArray(),
        MediaStore.Audio.Media.DEFAULT_SORT_ORDER

相关问题