Search code examples
androidandroid-mediaplayer

How to get songs and other media from an Album ID?


I want to get songs and other MEDIA details from Album Id. All I have is Album Id and I tried many solutions but none of them succeed.

My code snippet:

ContentResolver contentResolver = getContentResolver();
        Uri mediaUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, albumId);
        Log.wtf("SKJDBKJ", mediaUri.toString());
        Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);

        // if the cursor is null.
        if(mediaCursor != null && mediaCursor.moveToFirst())
        {
            Log.wtf("DSJK", "entered cursor");
            //get Columns
            int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM);
            int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
            int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);

            // Store the title, id and artist name in Song Array list.
            do
            {
                long thisId = mediaCursor.getLong(idColumn);
                String thisTitle = mediaCursor.getString(titleColumn);
                String thisArtist = mediaCursor.getString(artistColumn);
                Log.wtf("Title", thisTitle);

                // Add the info to our array.
                songArrayList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (mediaCursor.moveToNext());

            // For best practices, close the cursor after use.
            mediaCursor.close();
        }

Log for mediaUri returns path to current album, e.g. : content://media/external/audio/media/41. Someone tell me how do I do it?


Solution

  • I have figured it out myself after a LOT of trial and errors. I don't know if it's the best and safest way to do so, but as far as it's working I am happy.

    I changed my code a bit and compared Album IDs. Here's the snippet:

    ContentResolver contentResolver = getContentResolver();
            Uri mediaUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            Log.wtf("SKJDBKJ", mediaUri.toString());
            Cursor mediaCursor = contentResolver.query(mediaUri, null, null, null, null);
    
            // if the cursor is null.
            if(mediaCursor != null && mediaCursor.moveToFirst())
            {
                Log.wtf("DSJK", "entered cursor");
                //get Columns
                int titleColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
                int idColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media._ID);
                int artistColumn = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
                int albumId = mediaCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
    
                // Store the title, id and artist name in Song Array list.
                do
                {
                    long thisId = mediaCursor.getLong(idColumn);
                    long thisalbumId = mediaCursor.getLong(albumId);
                    String thisTitle = mediaCursor.getString(titleColumn);
                    String thisArtist = mediaCursor.getString(artistColumn);
    
                    // Add the info to our array.
                    if(this.albumId == thisalbumId)
                    {
                        Log.wtf("SAME2SAME", String.valueOf(thisalbumId));
                        Log.wtf("SAME2SAME", String.valueOf(this.albumId));
                        songArrayList.add(new Song(thisId, thisTitle, thisArtist));
                    }
                }
                while (mediaCursor.moveToNext());
    
                // For best practices, close the cursor after use.
                mediaCursor.close();
            }
    

    I changed:

    1. Albums to Media in MediaStore.Audio.Audio.xxx.
    2. Got the album Id of Media.
    3. Compared that to album Id I receive from bundle extras.
    4. Only add those songs in the arrayList. I guess this'll be the way for Artists too.