[英]Get cover of the songs in mediastore
我正在尝试使用以下代码获取歌曲的封面(专辑封面):
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 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.