检查专辑封面是否存在

检查专辑封面是否存在

本文介绍了检查专辑封面是否存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建音乐播放器应用程序.我正在尝试用歌曲的专辑封面填充recyclerView.我使用下面给出的代码成功做到了这一点.但是某些歌曲在文件夹中没有嵌入专辑封面,也没有任何专辑封面.因此,我试图在将专辑封面添加到recyclerView之前检查专辑封面是否为空.如果专辑封面为空,则应用程序将自动从互联网上获取专辑封面.我尝试检查专辑封面是否为空,但是每次它给我一个uri专辑封面是否可用的信息.

I'm building a music player app. I'm trying to populate a recyclerView with album arts of songs. I successfully did that with the code that is given below. But some of the songs do not have embedded album art not any album art in the folder. So, I'm trying to check if the album art is null or not before adding it to the recyclerView. If the album art is null, the app will automatically fetch it from the internet. I tried checking if the album art is null or not, but everytime it gives me a uri whether the album art is available or not.

从媒体存储区获取专辑封面的代码:

Code for fetching album art from mediastore:

 String[] projection = {MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Media.ALBUM,
            MediaStore.Audio.Media.DURATION,
            MediaStore.Audio.Media.DATA,
            MediaStore.Audio.Media.ALBUM_ID};
    Cursor cursor = getActivity().getContentResolver().query(uri, projection, selection, whereVal, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                long id = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                Long duration = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));
                String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                Long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));

                String albumArtUri = String.valueOf(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"),albumId));


 SongInfoModel s = new SongInfoModel(id, name, artist, null, album, null, duration, data, albumId,albumArtUri);
                SongList.add(s);



            } while (cursor.moveToNext());
        }

albumArtUri的值永远不会为空.

The value for albumArtUri is never null.

它始终提供以下uri:

It always gives the following uri(s):

content://media/external/audio/albumart/12
content://media/external/audio/albumart/5
content://media/external/audio/albumart/6
content://media/external/audio/albumart/3
content://media/external/audio/albumart/8
content://media/external/audio/albumart/9

但是在这些uri中,content://media/external/audio/albumart/9没有专辑封面.它总是显示我使用Glide

But among these uri(s), content://media/external/audio/albumart/9 has no album art. It always displays the placeholder I said using Glide

所以我的问题是,在填充recyclerView之前如何检查专辑封面是否可用(作为嵌入作品还是在歌曲文件夹中)?我知道我可以使用Glide占位符,并且正在使用它.但是,如果专辑封面无法脱机使用,我还想从互联网上获取专辑封面,这就是为什么我需要检查以便将互联网链接添加到数组列表中的原因.希望你们能明白我的意思.

So my question, how to check if album art is available(as embedded art or in song folder) before populating the recyclerView? I know I can use Glide placeholder and I'm using it. But I also want to fetch album art from the internet if album art is not available offline, that is why I need to check, so that I can add the internet links into the arraylist. Hope you guys get what I'm trying to say.

谢谢!

编辑:

我发现以下给定的代码片段会返回,无论是否存在专辑封面,但会占用大量时间.

I found that the below given code snippet returns if album art exists or not but it consumes a lot of time.

  public String fetchAlbumArt(long albumID){

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

    cur.moveToFirst();
    art = cur.getString(cur.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));

    cur.close();

    return art;
}

推荐答案

最简单的方法:

                //  loading album cover using Glide library
            String stralbumId = c.getString(c
                    .getColumnIndex(BaseColumns._ID));

            Uri ImageUrl = getAlbumUri(mContext, stralbumId);
            if (ImageUrl != null) {
                Glide.with(mContext)
                        .asBitmap()
                        .load(ImageUrl)
                        .into(image);
            }

  public Uri getAlbumUri(Context mContext,String album_id){
if(mContext!=null) {
    Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
    Uri imageUri = Uri.withAppendedPath(sArtworkUri, String.valueOf(album_id));
    return imageUri;
}
return null;

}

这篇关于检查专辑封面是否存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:15