Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>This was the original answer</strong></p> <p>i sorted the unique albums by checking if they had already been added to my map</p> <pre><code>public Map&lt;String, String&gt; getAlbumList(Context c) { //setup map and cursor Map&lt;String, String&gt; result = new HashMap&lt;String, String&gt;(); String selection = MediaStore.Audio.Media.IS_MUSIC + " !=0"; final Cursor mCursor = c.getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[] {MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM_ID,}, selection, null, "LOWER ("+MediaStore.Audio.Media.ALBUM + ") ASC"); int count = mCursor.getCount(); String[] mArtist = new String[count]; String[] mAlbum = new String[count]; String[] AlbumID = new String[count]; int i = 0; int j = 0; if (mCursor.moveToFirst()) { do { mAlbum[i] = mCursor .getString(mCursor .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM)); mArtist[i] = mCursor.getString(mCursor .getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)); AlbumID[i] = Long.toString(mCursor .getLong(mCursor .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID))); //checking for same previous value if(result.containsValue(mAlbum[i])){ }else{ result.put("artist" + j, mArtist[i]); result.put("album" + j, mAlbum[i]); result.put("AlbumID" + j, AlbumID[i]); j = j + 1; } i = i + 1; } while (mCursor.moveToNext()); } result.put("count", Integer.toString(j)); mCursor.close(); return result; } } </code></pre> <p>perhaps not the prettiest solution to unique sorting of the albums... but it works exactly as intended without fumbling around with sqlite....</p> <p>i'm passing in a context here because i'm using a listview in a fragment with a custom adapter without the activity context getContentResolver() doesn't work....</p> <p><strong>addendium to original answer</strong></p> <p>mArt, mData, mAlbums, and mArtists are Arraylists...</p> <pre><code>private void getList(View view){ mArt.clear(); mAlbums.clear(); mArtists.clear(); String[] projection = {"DISTINCT " + MediaStore.Audio.Media.ALBUM_ID, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.ARTIST } ; String[] projection2 = {"Distinct " + MediaStore.Audio.Albums.ALBUM, MediaStore.Audio.Albums.NUMBER_OF_SONGS}; Cursor mCursor = getActivity().getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, "0==0 ) GROUP BY (" + MediaStore.Audio.Media.ALBUM_ID, null, MediaStore.Audio.Media.ALBUM + " COLLATE NOCASE ASC"); Cursor mCursor2 = getActivity().getApplicationContext().getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, projection2, null, null, MediaStore.Audio.Albums.ALBUM + " COLLATE NOCASE ASC"); if(mCursor != null &amp;&amp; mCursor.getCount() &gt; 0 &amp;&amp; mCursor2 != null &amp;&amp; mCursor2.getCount() &gt; 0){ CursorJoiner joiner = new CursorJoiner(mCursor, new String[]{MediaStore.Audio.Media.ALBUM},mCursor2, new String[]{MediaStore.Audio.Albums.ALBUM}); for (CursorJoiner.Result joinerResult : joiner) { switch (joinerResult) { case LEFT: break; case RIGHT: break; case BOTH: String songs_s = "Song"; int tracks = mCursor2.getInt(mCursor2.getColumnIndex(MediaStore.Audio.Albums.NUMBER_OF_SONGS)); if (tracks &gt; 1) { songs_s = "Songs"; } mArtists.add(tracks + " " + songs_s + " By " + mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST))); mAlbums.add(mCursor2.getString(mCursor2.getColumnIndex(MediaStore.Audio.Albums.ALBUM))); mArt.add(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)))); } } } mCursor.close(); mCursor2.close(); } </code></pre> <p>the GROUP BY clause in the selection guarantees unique albums, since album art is sotred by the album id this line:</p> <pre><code>mArt.add(ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), mCursor.getInt(mCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)))); </code></pre> <p>makes sure that the album art and the album are in the same position...</p> <p>i am using a cursor joiner here so that I can get the artist, album, and number of songs in the album... this can only be returned from two separate queries...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload