我正在尝试使用以下代码获取歌曲的封面(专辑封面):
public static String getCoverArtPath(Context context, long androidAlbumId) {
String path = null;
Cursor c = context.getContentResolver().query(
MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID + "=?",
new String[]{Long.toString(androidAlbumId)},
null);
if (c != null) {
if (c.moveToFirst()) {
path = c.getString(0);
}
c.close();
}
return path;
}
此方法返回图像路径的字符串,但此路径指向未格式化的文件。 如何在 ImageView 上设置此图像? 如果您知道除此方法之外的任何其他方法,请告诉我。
最佳答案
你可以从路径创建一个drawable并设置它
Drawable drawable = Drawable.createFromPath(path_of_the_cover_art);
yourImageView.setImageDrawable(drawable);
或者创建一个文件:
File image = new File(path_of_the_cover_art);
if(image.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath());
yourImageView.setImageBitmap(bitmap);
}
只要确保您拥有 WRITE_STORAGE_PERMISSION 即可!
关于java - 获取媒体商店中歌曲的封面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58696381/