我正在尝试使用以下代码获取歌曲的封面(专辑封面):
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.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!