1

I want cover album for songs in my phone but I get colum index -1

        int imageColumn = musicCursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);

This is the full code:

// Retrieve song info from device
public void getSongList() {
    String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

    // Query external audio
    ContentResolver musicResolver = getActivity().getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor musicCursor = musicResolver.query(musicUri, null, selection, null, null);

    // Iterate over results if valid
    if (musicCursor != null && musicCursor.moveToFirst()) {
        // Get columns
        int titleColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media._ID);
        int artistColumn = musicCursor.getColumnIndex
                (android.provider.MediaStore.Audio.Media.ARTIST);
        int durationColumn = musicCursor.getColumnIndex
                (MediaStore.Audio.Media.DURATION);

        int imageColumn = musicCursor.getColumnIndex
                (MediaStore.Audio.AlbumColumns.ALBUM_ART);
        // ****** HERE I HAVE -1, the same is with MediaStore.Audio.Albums.ALBUM_ART ******


        // Add songs to list
        do {
            long thisId = musicCursor.getLong(idColumn);
            String thisTitle = musicCursor.getString(titleColumn);
            String thisArtist = musicCursor.getString(artistColumn);
            long thisDuration = musicCursor.getLong(durationColumn);

            String thisPathAlbumImage =  musicCursor.getString(imageColumn); 
            // ****** HERE MY APP CRASH FOR -1 INDEX ******

            arrayOfSongs.add(new Song(thisId, thisTitle, thisArtist, thisDuration, thisPathAlbumImage));

            Log.d(LOG_TAG, "New song added: " + thisTitle);
        }
        while (musicCursor.moveToNext());
    }
}

Why I get -1? I want each album cover to set them in a ListView with song title, artist name and duration.

Nammen8
  • 619
  • 1
  • 11
  • 31

2 Answers2

1

You should use Uri.parse("content://media/external/audio/albumart"); to query the albumart. You can have more information at this StackOverflow post.

Community
  • 1
  • 1
Nawako
  • 342
  • 3
  • 17
1

The query that you are using does not return the column MediaStore.Audio.AlbumColumns.ALBUM_ART. You now have certain options.

  1. You can use the answer provided and linked by @nawako. But as this tweet from Ian Lake reminds us, it is not explicitly part of the official SDK could be changed or modified.
  2. You can use a query that returns the column. Using MediaStore.Audio.Albums.EXTERNAL_CONTENT_TYPE will retrieve a list of albums, as well as include MediaStore.Audio.AlbumColumns.ALBUM_ART. Please note, this option does not load any music, you will need a separate query for that.
  3. You can retrieve the cover from the using using the method in this answer. This does not involve any modification of your current query.

Here is some sample code implementing option 2.

Bitmap art;
String path;
Uri artUri;
Uri musicUri = android.provider.MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI;

.
.
.

path = musicCursor.getString(imageColumn);

if (path != null) {
    artUri = Uri.fromFile(new File(path));

    try {
         coverArt = MediaStore.Images.Media.getBitmap(musicResolver, artUri);
    } catch (IOException e) {
        // For some reason we have no album art. Assign a default cover?
        e.printStackTrace();
    }
}
Community
  • 1
  • 1
phxhawke
  • 2,581
  • 23
  • 17
  • Hi, thanks for your reply... the best way is the number 2? Any examples? Thanks again :) – Nammen8 Nov 04 '15 at 18:49
  • if imageColumn is "imageColumn = musicCursor.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ART);" I always get -1 index :( – Nammen8 Nov 05 '15 at 11:25
  • I try with "int imageColumn2 = musicCursor2.getColumnIndex (MediaStore.Audio.Albums.ALBUM_ART);", index is 9 but it always crash: "CursorIndexOutOfBoundsException: Index -1 requested, with a size of 10"... but wait, it's better if i can extrat cover album in each song, i don't want all album because then I have to connect each song with each cover song, I have only a song list and I want cover album for each song... – Nammen8 Nov 05 '15 at 11:34