获取专辑封面从音频文件的Uri [英] Getting album art from the audio file Uri

查看:1153
本文介绍了获取专辑封面从音频文件的Uri的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从音频文件的Uri的专辑封面,这是我的code:

I am trying to get the album art from the audio file Uri, here is my code:

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

这个函数总是返回内容不得入内://媒体/外部/音频/ albumart / 0

This function always returns "No entry for content://media/external/audio/albumart/0"

推荐答案

我觉得你的问题就出在appendId

I think your problem lies in the appendId

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

与专辑的长_id取代Column_Index中,而不是列索引这对于_id为0。

replace column_index with Long _id of album rather than a column index which for _id is 0.

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

c是我的光标

这篇关于获取专辑封面从音频文件的Uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆