12

我正在尝试构建一个 MP3 播放器,我希望 ImageView 显示相应歌曲的专辑封面。我尝试了以下方法,但它不起作用。

albumcover = (ImageView) findViewById(R.id.cover);

String coverPath = songsList.get(songIndex).get(MediaStore.Audio.Albums.ALBUM_ART);
Drawable img = Drawable.createFromPath(coverPath);
albumcover.setImageDrawable(img);

当我尝试播放歌曲时,我得到的只是 ImageView 中的一个空白屏幕。

4

7 回答 7

26

以下是我获取歌曲专辑封面的方法:

Cursor cursor = getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, 
                new String[] {MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART}, 
                MediaStore.Audio.Albums._ID+ "=?", 
                new String[] {String.valueOf(albumId)}, 
                null);

if (cursor.moveToFirst()) {
    String path = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
    // do whatever you need to do
}

albumId指的MediaStore.Audio.Media.ALBUM_ID是那首歌。

如果您正在寻找特定歌曲的专辑封面(而不是在专辑列表中),据我所知,这是一个两阶段的过程,因为ALBUM_ART它是歌曲元数据的属性MediaStore.Audio.Albums并且不能直接作为歌曲元数据使用。

于 2013-07-10T15:22:48.867 回答
9

如果您有专辑 ID,您将获得专辑图片 uri:-

final public static Uri sArtworkUri = Uri
            .parse("content://media/external/audio/albumart");      

Uri uri = ContentUris.withAppendedId(PlayerConstants.sArtworkUri,
                listOfAlbums.get(position).getAlbumID());

如果你有一个图像 uri,你可以使用任何图像加载器GlidePicasoUIL来显示图像。

                               **OR**

您可以编写自己的图像加载器

public Bitmap getAlbumart(Context context, Long album_id) {
    Bitmap albumArtBitMap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    try {

        final Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");

        Uri uri = ContentUris.withAppendedId(sArtworkUri, album_id);

        ParcelFileDescriptor pfd = context.getContentResolver()
                .openFileDescriptor(uri, "r");

        if (pfd != null) {
            FileDescriptor fd = pfd.getFileDescriptor();
            albumArtBitMap = BitmapFactory.decodeFileDescriptor(fd, null,
                    options);
            pfd = null;
            fd = null;
        }
    } catch (Error ee) {
    } catch (Exception e) {
    }

    if (null != albumArtBitMap) {
        return albumArtBitMap;
    }
    return getDefaultAlbumArtEfficiently(context.getResources());
}



public Bitmap getDefaultAlbumArtEfficiently(Resources resource) {

    if (defaultBitmapArt == null) {

        defaultBitmapArt = decodeSampledBitmapFromResource(resource,
                R.drawable.default_album_art, UtilFunctions
                        .getUtilFunctions().dpToPixels(85, resource),
                UtilFunctions.getUtilFunctions().dpToPixels(85, resource));

    }
    return defaultBitmapArt;
}
于 2016-01-17T06:33:17.760 回答
8
 ContentResolver musicResolve = getContentResolver();
    Uri smusicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;
    Cursor music =musicResolve.query(smusicUri,null         //should use where clause(_ID==albumid)
        ,null, null, null);



    music.moveToFirst();            //i put only one song in my external storage to keep things simple
    int x=music.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART);
        String thisArt = music.getString(x);


     Bitmap bm= BitmapFactory.decodeFile(thisArt);
        ImageView image=(ImageView)findViewById(R.id.image);
        image.setImageBitmap(bm);
于 2016-08-10T12:41:31.303 回答
3

此方法返回带有歌曲路径和专辑封面的 ArrayList。

public static ArrayList<CommonModel> getAllMusicPathList(Context context) {
    ArrayList<CommonModel> musicPathArrList = new ArrayList<>();
    Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    Cursor cursorAudio = context.getContentResolver().query(songUri, null, null, null, null);
    if (cursorAudio != null && cursorAudio.moveToFirst()) {

        Cursor cursorAlbum;
        if (cursorAudio != null && cursorAudio.moveToFirst()) {

            do {
                Long albumId = Long.valueOf(cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
                cursorAlbum = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                        new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                        MediaStore.Audio.Albums._ID + "=" + albumId, null, null);

                    if(cursorAlbum != null && cursorAlbum.moveToFirst()){
                        String albumCoverPath = cursorAlbum.getString(cursorAlbum.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
                        String data = cursorAudio.getString(cursorAudio.getColumnIndex(MediaStore.Audio.Media.DATA));
                        musicPathArrList.add(new CommonModel(data,albumCoverPath ,false));
                    }

                 } while (cursorAudio.moveToNext());
        }
    }
    return musicPathArrList;
}

这是 CommonModel 。

public class CommonModel {

private String path;
private boolean selected;

public String getAlbumCoverPath() {
    return albumCoverPath;
}

public void setAlbumCoverPath(String albumCoverPath) {
    this.albumCoverPath = albumCoverPath;
}

private String albumCoverPath;

public CommonModel(String path, String albumCoverPath, boolean b) {
    this.path = path;
    this.albumCoverPath=albumCoverPath;
    this.selected=b;
}

public String getPath() {
    return path;
}

public void setPath(String path) {
    this.path = path;
}

public boolean getSelected() {
    return selected;
}

public void setSelected(boolean selected) {
    selected = selected;
}
}
于 2017-12-21T09:50:14.013 回答
0

你应该使用 Uri.parse("content://media/external/audio/albumart"); 查询专辑封面。第一个答案可能会在某些电话上出现异常(至少是我的)

于 2015-04-24T14:00:47.470 回答
0

示例代码

public static Uri getAlbumArtUri(long albumId) {
    return ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), albumId);
}


ArrayList<Uri> albumArtUris = new ArrayList<>();
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{"album_id"}, null, null, null);
cursor.moveToFirst();
do {
    long ablumId = cursor.getLong(cursor.getColumnIndexOrThrow("album_id"));
    albumArtUris.add(getAlbumArtUri(ablumId));
} while (cursor.moveToNext());
于 2018-10-16T08:49:03.743 回答
0

下面的代码对我有用。我知道它已经被回答了,它可能对检查参考的人有用。

 public void getAlbumArt() {
    try {
        ContentResolver cr = getContentResolver();
        Uri uri = MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

        Cursor cursor = cr.query(uri, null, null, null, null);

        if (cursor != null && cursor.moveToFirst()) {
            int albumart = cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART);
            do {
                String Albumids = cursor.getString(albumart);
                Albumid.add(Albumids);

            } while (cursor.moveToNext());
        }cursor.close();
    } catch (NumberFormatException e){
        e.printStackTrace();
    }
}
public void getSelectedPath(){
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            String path= String.valueOf(listView.getItemAtPosition(i));

                if(path.equals("null")){
                ImageView imgv=(ImageView)view.findViewById(R.id.imageView);
                imgv.setImageResource(R.drawable.unknowalbum);
                imgv.setMaxHeight(50);
                imgv.setMaxWidth(50);

            }
            else{
                Drawable image=Drawable.createFromPath(path);
                ImageView imgview=(ImageView)view.findViewById(R.id.imageView);
                imgview.setImageDrawable(image);

            }
        }
    });
}

有关完整代码,请访问 http://vasistaguru.blogspot.com/2017/02/get-albumart-and-trackname-using.html

于 2017-02-17T05:42:37.220 回答