其他分享
首页 > 其他分享> > android-从音频文件Uri获取专辑封面

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

作者:互联网

我正在尝试从音频文件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;
}

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

解决方法:

我认为您的问题出在appendId

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

用专辑的Long _id代替column_index,而不是_id为0的列索引.

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

c是我的光标

标签:mediastore,albumart,audio,android
来源: https://codeday.me/bug/20191121/2049360.html