从音频文件 Uri 获取专辑封面

2023-12-29

我正在尝试从音频文件 Uri 中获取专辑封面,这是我的代码:

// uri is the audio file uri

public static Bitmap getSongCoverArt(Context context, Uri uri){
    Bitmap songCoverArt = null;
    String[] projections = {MediaStore.Audio.Media.ALBUM_ID};
    Cursor cursor = null;
    try {
        cursor = context.getContentResolver().query(uri, projections, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
        cursor.moveToFirst();

        Uri songCover = Uri.parse("content://media/external/audio/albumart");
        Uri uriSongCover = ContentUris.withAppendedId(songCover, column_index);
        Log.d(TAG, uriSongCover.toString());
        ContentResolver res = context.getContentResolver();
        try {
            InputStream in = res.openInputStream(uriSongCover);
            songCoverArt = BitmapFactory.decodeStream(in);
        }catch (FileNotFoundException e){
            Log.e(TAG, e.getMessage());
        }
    }finally {
        if(cursor != null){
            cursor.close();
        }
    }

    return songCoverArt;
}

此函数始终返回“没有内容条目://media/external/audio/albumart/0”


我认为你的问题在于appendId

   Uri songCover = Uri.parse("content://media/external/audio/albumart");
    Uri uriSongCover = ContentUris.withAppendedId(songCover, column_index)

将column_index替换为专辑的长_id,而不是_id为0的列索引。

 album_id = c.getLong(c.getColumnIndex(MediaStore.Audio.Albums._ID));

其中 c 是我的光标

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从音频文件 Uri 获取专辑封面 的相关文章

随机推荐