获取媒体商店中歌曲的封面

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;
}

此方法returns 图像路径的字符串,但此路径指向未格式化的文件。 如何在 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 即可!