如何使用Glide加载媒体的专辑封面?

huangapple go评论66阅读模式
英文:

How to load media's album cover with glide?

问题

I have songList[position].id as my MediaStore ID. I tried below method to read from album art but it didn't work.

val artworkUri = Uri.parse("content://media/external/audio/albumart")
val path = ContentUris.withAppendedId(artworkUri, songList[position].id)
Glide.with(holder.songCover.context).load(songList[position].path.toUri()).into(holder.songCover)

I also tried Glide.with(holder.songCover.context).loadFromMediaStore(), but it seems that this method doesn't exist.

英文:

So I have songList[position].id as my MediaStore ID. I tried below method to read from albumart but it didn't work.

val artworkUri = Uri.parse("content://media/external/audio/albumart")
        val path = ContentUris.withAppendedId(artworkUri, songList[position].id)
        Glide.with(holder.songCover.context).load(songList[position].path.toUri()).into(holder.songCover)

I also tried Glide.with(holder.songCover.context).loadFromMediaStore(), but it seems that this method doesn't exist.

答案1

得分: 0

确保在你的 AndroidManifest.xml 文件中拥有必要的权限,并在你的应用程序中获取这些权限。

使用以下代码:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

然后,修改你的代码如下:

val artworkUri = Uri.parse("content://media/external/audio/albumart")
val albumArtUri = ContentUris.withAppendedId(artworkUri, songList[position].id)
Glide.with(holder.songCover.context)
     .load(albumArtUri)
     .placeholder(R.drawable.placeholder) // 在加载时可选的占位图像
     .error(R.drawable.error) // 在加载失败时可选的错误图像
     .into(holder.songCover)
英文:

make sure you have the necessary permissions in your AndroidManifest.xml and get permissions in your app

use this :

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

then modify your code as follows:

 val artworkUri = Uri.parse("content://media/external/audio/albumart")
val albumArtUri = ContentUris.withAppendedId(artworkUri, songList[position].id)
Glide.with(holder.songCover.context)
     .load(albumArtUri)
     .placeholder(R.drawable.placeholder) // Optional placeholder image while loading
     .error(R.drawable.error) // Optional error image if loading fails
     .into(holder.songCover)

huangapple
  • 本文由 发表于 2023年5月20日 23:14:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76295925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定