英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论