javaandroidmediastorealbumart

Mediastore albums and media columns


Too retrieve the album id i use:

String SONG_ALBUMID  = MediaStore.Audio.Albums.ALBUM_ID;

But what is the difference between MediaStore.Audio.Albums.ALBUM_ID and MediaStore.Audio.Media.ALBUM_ID

Thanks,


Solution

  • Here are 2 approaches: (include the requested columns in your resolver.query)

         public String getalbum_id(Cursor pcursor) {
        int album_col = pcursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);
        String album_id = pcursor.getString(album_col);
    
        if (album_id.length() > 0) {
            return album_id;
        } else {
            return "";
        }
    }
    
    public String getAlbumIdfromPath(Context context, String datapath) {
        ContentResolver cr = context.getContentResolver();
        final String _id = MediaStore.Audio.Media._ID;
        final String path = MediaStore.Audio.Media.DATA;
        final String album_id = MediaStore.Audio.Media.ALBUM_ID;
        final String[] columns = {_id, album_id};
        final String[] trackpath = {"%" + datapath + "%"};
        String where = path + " LIKE ?";
        String stralbum_id = null;
        Cursor crs = cr.query(uri, columns, where, trackpath, null);
        if (crs != null && crs.moveToFirst()) {
            stralbum_id = crs.getString(crs.getColumnIndexOrThrow(album_id));
            crs.close();
        }
        return stralbum_id;
    }
    

    now to display, I have posted previously but I use the Gilde library. It does all the hard work.

                //  loading album cover using Glide library
            String artist_id = (c.getString(c
                    .getColumnIndex(MediaStore.Audio.Artists._ID)));
            Cursor albs = albums.getArtistsAlbumcursor(mContext, artist_id);
            String stralbumId=null;
            if(albs!=null && albs.moveToFirst()){
               stralbumId= albs.getString(albs.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
            }
            Uri ImageUrl = plist.getAlbumUri(mContext, stralbumId);
            if (ImageUrl != null) {
                Glide.with(mContext)
                        .asBitmap()
                        .load(ImageUrl)
                        .into(image);
            }
        }
    

    where getAlbumuri is:

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

    Include the following in your module build.gradle

        implementation 'com.github.bumptech.glide:glide:4.5.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
    
       private final Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;