Nicholas
Nicholas

Reputation: 1980

Performing Album Art Checks on an Audio file

I've been attempting to look for a solution with regards to the problem as stated in the title above but to no avail.

I'm trying to find a way to check if an Audio file has or does not have an album art, so in the case when there is no album art, I'll be able to set my own drawable for it.

Here's the code I'm running to initialize my song list.

ContentResolver cr = getActivity().getContentResolver();

        Uri songsUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String songsSelection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
        String songsSortOrder = MediaStore.Audio.Media.TITLE + " ASC";
        Cursor songCur = cr.query(songsUri, null, songsSelection, null, songsSortOrder);
        int songCount = 0;

        if(songCur != null)
        {
            songCount = songCur.getCount();

            if(songCount > 0)
            {
                while(songCur.moveToNext())
                {
                    //String data = songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.DATA));

                    // Debug
                    //Log.e("Song Path", data);

                    // (long _id, long _albumId, long _artistId, String _title,
                    // String _artistName, String _albumName, int _duration)
                    //Log.e("Music ID", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media._ID)));
                    //Log.e("Music Album ID", songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)));
                    //Log.e("Music Artist ID", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)));
                    //Log.e("Music Title", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.TITLE)));
                    //Log.e("Music Artist Name", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.ARTIST)));
                    //Log.e("Music Album Name", songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM)));
                    //Log.e("Music Duration", songCur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DURATION)));

                        mediaManager.songFiles.add(new Song(
                                songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.DATA)),
                                songCur.getLong(songCur.getColumnIndex(MediaStore.Audio.Media._ID)),
                                songCur.getLong(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)),
                                songCur.getLong(songCur.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)),
                                songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.TITLE)),
                                songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
                                songCur.getString(songCur.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
                                songCur.getInt(songCur.getColumnIndex(MediaStore.Audio.Media.DURATION))));

                }

            }

            songCur.close();
        }

Let me know if you require any further explanation on my question.

Upvotes: 0

Views: 140

Answers (1)

KudzieChase
KudzieChase

Reputation: 261

I noticed in your code that you're getting retrieving the album_id of a particular song so a while back I discovered a simple way to fetch album art by appending the album_id to a particular URI, checkout the following code

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

I then use an Image Loading library like Universal Image Loader or Glide to load the image into an ImageView, like so:

ImageLoader.getInstance().displayImage
            (getAlbumArtUri(song.albumId).toString(),
                    holder.albumArt, new DisplayImageOptions.Builder().cacheInMemory(true).
                            showImageOnFail(R.drawable.albumart_mp_unknown).resetViewBeforeLoading(true).
                            build());

showImageOnFail() takes in a drawable that would be displayed incase of error.(This means the song doesn't have album artwork)

P.S I also found the solution discussed on this Stack Overflow Post Quite useful:

Most robust way to fetch Album Art in Android

Hope this helps!

Upvotes: 2

Related Questions