How can I display Album Art using MediaStore.Audio.Albums.ALBUM_ART?

大兔子大兔子 提交于 2019-12-17 02:34:06

问题


I'm trying to build a MP3 player and I want the ImageView to display the album art of respective songs. I've tried the following, but it doesn't work.

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

When I try to play the songs, all I get is a blank screen in the ImageView.


回答1:


Here's how I get album art for a song:

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 refers to MediaStore.Audio.Media.ALBUM_ID for that song.

If you're are looking for album art for a particular song (rather than in a list of albums), as far as I know it's a two-stage process since ALBUM_ART is a property of MediaStore.Audio.Albums and is not available directly as song metadata.




回答2:


If you have album ID you get Album Image uri :-

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

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

And if you have a Image uri you can use any of the image loader Glide, Picaso, UIL to display images .

                               **OR**

you can write your own image loader

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



回答3:


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



回答4:


This method return ArrayList with song path and album art.

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

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



回答5:


You should use Uri.parse("content://media/external/audio/albumart"); to query the albumart. the 1st answer may get exception on some phone (at least mine)




回答6:


The below code worked for me. I know it has been answered already, it may be useful for someone checking for reference.

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

            }
        }
    });
}

for complete code visit http://vasistaguru.blogspot.com/2017/02/get-albumart-and-trackname-using.html




回答7:


Example Code

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());


来源:https://stackoverflow.com/questions/17573972/how-can-i-display-album-art-using-mediastore-audio-albums-album-art

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!