4

So I've been trying this for some time now. To get the album art for an mp3 file and displaying it on its respective ImageView and I am using the uri("content://media/external/audio/albumart")

This is my method for getting the album art

    public Bitmap getAlbumArt(long idAlbum){
    Bitmap bitmap = null;

    try{
        final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
        Uri uri = ContentUris.withAppendedId(sArtworkUri, idAlbum);
        ParcelFileDescriptor parcelFileDescriptor = getContext().getContentResolver().openFileDescriptor(uri,"r");
        if (parcelFileDescriptor != null){
            FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
            bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
        }

    }catch (Exception e){
        e.printStackTrace();
    }
    return bitmap;
    } 

and this method always returns

java.io.FileNotFoundException: No entry for content://media/external/audio/albumart/31726

where the 31726 is the album id.

Since I'm catching this exception and I set it to a default Album art if it returns null, every mp3 has its ImageView set to the default album art. I am using my Samsung Galaxy s3 to run the application and my device runs android 4.2.2 JellyBean. Please someone help me getting this right.

This is how I request the album id

    Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String[] columns = {
            MediaStore.Audio.Media.TITLE,
            MediaStore.Audio.Media.ARTIST,
            MediaStore.Audio.Albums._ID,
            MediaStore.Audio.Media.DATA
    };

    String where = MediaStore.Audio.Media.IS_MUSIC + "=1";

    Cursor musicCursor = usicResolver.query(musicUri,columns,where,null,    null);

Then in an if loop with condition

    if(musicCursor.moveToFirst()){
    int albumId = musicCursor.getColumnIndex
                (MediaStore.Audio.Albums._ID);
    do{
          long idAlbum = musicCursor.getLong(albumId);

          //Then i send it to my above method getAlbumArt
          Bitmap songAlbumArt = getAlbumArt(idAlbum);

      }while(musicCursor.moveToNext());

     }
Dev24601
  • 63
  • 1
  • 7
  • 'the 31726 is the album id.'. Where did you get that id from? Why do you think your code should work? – greenapps Nov 09 '15 at 08:26
  • 'I am using my Samsung Galaxy s3 as my emulator'. ???? – greenapps Nov 09 '15 at 08:27
  • @greenapps I'm sorry i meant using my real device and not an emulator. I'm getting the album id using MediaStore.Audio.Albums._ID – Dev24601 Nov 09 '15 at 08:46
  • @greenapps Most of the people are using the same technique for retrieving album art and it seems it is working pretty well for them. – Dev24601 Nov 09 '15 at 08:55
  • Please show how you request and decode those id's. – greenapps Nov 09 '15 at 11:45
  • Instead of that parcel file descripter try: `InputStream is = getContext().getContentResolver().openInputStream(uri);`. BitmapFactory can decode from an input stream. – greenapps Nov 09 '15 at 12:05
  • @greenapps No using InputStream did not work for me either. The logcat goes on without stopping, repeating the line V/AlarmManager: waitForAlarm result :8 and with the same exception no entry for content – Dev24601 Nov 09 '15 at 15:01
  • @greenapps I have added how I request the album Id above...this is the exception content://media/external/audio/albumart/31726 – Dev24601 Nov 09 '15 at 15:08
  • 1
    I see a problem in your query above. To get the album Id of an audio medium you need to query the column MediaStore.Audio.Media.ALBUM_ID . You are querying for MediaStore.Audio.Albums._ID which will give you the media id instead of the album id. That's because MediaStore.Audio.Albums._ID has a constant value of "_id" and MediaStore.Audio.Media.ALBUM_ID has a constant value of "album_id". With the correct album id the method posted in my answer below should work. Maybe your solution is then working as well. – Emanuel Seidinger Nov 10 '15 at 09:00

3 Answers3

2

This is how I query the cover art path from the album id:

private static String getCoverArtPath(long albumId, Context context) {

    Cursor albumCursor = context.getContentResolver().query(
            MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Audio.Albums.ALBUM_ART},
            MediaStore.Audio.Albums._ID + " = ?",
            new String[]{Long.toString(albumId)},
            null
    );
    boolean queryResult = albumCursor.moveToFirst();
    String result = null;
    if (queryResult) {
        result = albumCursor.getString(0);
    }
    albumCursor.close();
    return result;
}

You can get the Bitmap from the BitmapFactory using the path returned from the method above:

BitmapFactory.decodeFile(coverArtPath);
Emanuel Seidinger
  • 1,272
  • 1
  • 12
  • 21
  • Not working for me. It does not give any error just some frames skipped but that is not a big deal. I have noticed that where I am checking if my Bitmap is null android studio prompts that the Bitmap is always going to be null. Any idea of what I may be doing wrong ? and i checked if anywhere locally i have set the bitmap to be null but no i have not found any thing like that. – Dev24601 Nov 09 '15 at 15:41
  • I don't see why the Bitmap should always be null. I just tried to get the Bitmap from the content uri with appended id like you did in your question above. I also get FileNotFoundExceptions with my albums. – Emanuel Seidinger Nov 09 '15 at 15:46
  • So right now using your method i have two cursors i.e. the one i have been already using for querying the album id, title , artist and the other one mentioned in your above method. This wont be a problem right using two cursors ? – Dev24601 Nov 09 '15 at 15:56
  • I don't think that is a problem. Before returning the path I close the cursor. – Emanuel Seidinger Nov 09 '15 at 16:03
  • 1
    I found that my albumCursor.getCount() is always 0. That means there is something wrong with the query being passed. – Dev24601 Nov 10 '15 at 06:11
  • There seems to be a problem in your query for the album id. Please see my comment below your question. – Emanuel Seidinger Nov 10 '15 at 09:02
  • It seems that MediaStore.Audio.Albums.ALBUM_ART was deprecated on API 29. Take a look at my response above. – droid256 Jun 11 '22 at 02:32
0
ImageLoader.getInstance().
    displayImage( ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), song_tag.albumId).toString(),
            image,
            new DisplayImageOptions.Builder().cacheInMemory(true).showImageOnFail(R.drawable.stock6).resetViewBeforeLoading(true).build());

image is the object of ImageView

song_tag.albumid is the album id which you have to send to get the image

stock6 is the default image

Pang
  • 9,564
  • 146
  • 81
  • 122
0

EDIT:

It seems that ALBUM_ART was deprecated on API 29: https://developer.android.com/reference/android/provider/MediaStore.Audio.AlbumColumns#ALBUM_ART

Instead, they suggest using ContentResolver#loadThumbnail: https://developer.android.com/reference/android/content/ContentResolver#loadThumbnail(android.net.Uri,%20android.util.Size,%20android.os.CancellationSignal)

--

ORIGINAL:

Querying the album art field from content://media/external/audio/albums would do it for me some time ago, but I noticed it stopped working since a few Android versions ago, for some reason.

Now I querying the albums table gets me a null album art field:

adb shell content query --uri content://media/external/audio/albums
Row: 0 numsongs=1, artist=My Artist, numsongs_by_artist=1, _id=8821305607184940112, album=My Album, album_art=NULL, album_key=4c3a3434044e5052303a4604342a3e32044248194e, artist_id=7053871990187004266, artist_key=484c465046503832464c2a04422a402a563a32444e3a4e, maxyear=NULL, minyear=NULL, album_id=8821305607184940112

Querying the album art route directly, also gives me no results...

adb shell content query --uri content://media/external/audio/albumart
No result found.

It seems that the MediaStore on the platform side... stopped trying to parse album art from music files, and add them to its db?

droid256
  • 404
  • 5
  • 15