1

I want to retrieve images for all songs in my list but i am getting only one image for all songs. Please tell me where i am wrong.

ContentResolver musicResolver = getContentResolver();
    Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor cursor = musicResolver.query(musicUri, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        int titleColumn = cursor
                .getColumnIndex(android.provider.MediaStore.Audio.Media.TITLE);
        int idColumn = cursor
                .getColumnIndex(android.provider.MediaStore.Audio.Media._ID);
        Long albumId = cursor.getLong(cursor
                .getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
        int artistColumn = cursor
                .getColumnIndex(android.provider.MediaStore.Audio.Media.ARTIST);
        do {

            Songs song = new Songs();
            song.id = cursor.getLong(idColumn);
            song.title = cursor.getString(titleColumn);
            song.artist = cursor.getString(artistColumn);
            Uri sArtworkUri = Uri
                    .parse("content://media/external/audio/albumart");
            Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri,
                    albumId);

            song.imagePath = albumArtUri;

            songList.add(song);

        } while (cursor.moveToNext());
    }
}

I am trying to put it in listview so here is adapter:-

public View getView(int arg0, View arg1, ViewGroup arg2) {
         LinearLayout songLay = (LinearLayout)songInf.inflate
                  (R.layout.cust_list_song, arg2, false);
              //get title and artist views
              TextView songView = (TextView)songLay.findViewById(R.id.song_title);
              TextView artistView = (TextView)songLay.findViewById(R.id.song_artist);
              ImageView imageSong = (ImageView) songLay.findViewById(R.id.song_cover);
              Songs currSong = songs.get(arg0);
              songView.setText(currSong.title);
              artistView.setText(currSong.artist);

              Bitmap bitmap = null;
              try {
                  bitmap = MediaStore.Images.Media.getBitmap(
                          c.getContentResolver(), currSong.imagePath);
                  bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true);
                  imageSong.setImageBitmap(bitmap);
              } catch (FileNotFoundException exception) {
                  exception.printStackTrace();
              } catch (IOException e) {

                  e.printStackTrace();
              }

              songLay.setTag(arg0);
              return songLay;             
    }

I am beginner to this, so need help.

user6556461
  • 377
  • 3
  • 14
  • There must be a reason, if the CD cover is called "AlbumArt" and not "SongArt"... Do you want every song to have a different picture?! Then ask the artist to pay their graphician to make a picture for every song. But you would pay the CD twice as much, I guess. – Phantômaxx Oct 21 '16 at 08:26
  • Yeah it's albumart I know but the problem is in every list item there is only one bitmap – user6556461 Oct 21 '16 at 08:29
  • I'm happy to have all the songs which belong to an album have the same album picture. I don't see much of an issue, here. – Phantômaxx Oct 21 '16 at 08:36

1 Answers1

0

As I can see in your code, that's obvious result since you use the album's image instead of song image

Uri sArtworkUri = Uri
                .parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri,
                albumId); 
song.imagePath = albumArtUri;// You set the album art uri as song's image path