How can i fetch the album art from all songs and add it to an imageView?

395 Views Asked by At
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.display_songs);

    recyclerView = findViewById(R.id.recyclerView);
    if (recyclerView != null) {
        recyclerView.setHasFixedSize(true);
    }
    songAdapter = new SongAdapter(this, songList);
    recyclerView.setAdapter(songAdapter);
    songAdapter.notifyDataSetChanged();
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
    DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation());
    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.addItemDecoration(dividerItemDecoration);

    mainLayout = findViewById(R.id.mainLayout);
    mToolbar = findViewById(R.id.mToolbar);
    setSupportActionBar(mToolbar);

    if (getSupportActionBar() != null) {
        getSupportActionBar().setTitle(R.string.library);
    }

    currSongLayout = findViewById(R.id.currSongLayout);
    currSongTitle = findViewById(R.id.currSongTitle);
    currSongArtist = findViewById(R.id.currSongArtist);
    albumArt = findViewById(R.id.albumIv);


    recyclerView.addOnItemTouchListener(new OnItemClickListeners(this, new OnItemClickListeners.OnItemClickListener() {
        @TargetApi(Build.VERSION_CODES.O)
        @Override
        public void onItemClick(View view, int position) {
            songIndex = position;
            Toast.makeText(getApplicationContext(), "You Clicked position: " + songIndex + " " +  songList.get(songIndex).getData(), Toast.LENGTH_SHORT).show();
            try {
                mediaPlayer.reset();
                mediaPlayer.setDataSource(songList.get(songIndex).getData());
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mp.start();
                    }
                });

                currSongTitle.setText(songList.get(position).getTitle());
                currSongArtist.setText(songList.get(position).getArtist());
            }catch (Exception e){

                e.printStackTrace();

            }
            currSongLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent currSong = new Intent(DisplaySongsActivity.this, SongActivity.class);
                    currSong.putExtra("songIndex", songIndex);
                    startActivity(currSong);
                }
            });

            String path = songList.get(position).getAlbum();
            if (path.equals("null")){
                albumArt.setImageResource(R.drawable.no_album);
            }else{
                Drawable image = Drawable.createFromPath(path);
                albumArt.setImageDrawable(image);
            }


        }
    }));


    //Methode call controle voor toestemming lezen Storage
    checkUserPermission();

    //Methode call songs inlezen
    getSongs();

    //Sort songs
    Collections.sort(songList, new Comparator<QuerySongs>() {
        public int compare(QuerySongs a, QuerySongs b) {
            return a.getTitle().compareTo(b.getTitle());
        }
    });

}
//Songs inlezen
@TargetApi(Build.VERSION_CODES.O)
public void getSongs() {

    Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    Cursor myCursor = getContentResolver().query(songUri, null, selection, null, null);


    if (myCursor != null && myCursor.moveToFirst()) {
        int id_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID);
        int data_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        int title_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
        int artist_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);
        int album_Column = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);
        long albumId = myCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID);

        do {

            Long songId = myCursor.getLong(id_Column);

            String songData = myCursor.getString(data_Column);
            String songName = myCursor.getString(title_Column);
            String songArtist = myCursor.getString(artist_Column);
            String songAlbum = myCursor.getString(album_Column);

            QuerySongs querySongs = new QuerySongs(songId, songData, songName, songArtist, songAlbum);
            songList.add(querySongs);

        } while (myCursor.moveToNext());
        myCursor.close();
    }
}

}

Song constructor class;

public QuerySongs(Long songId, String songData,  String songName, String songArtist, String songAlbum){
    this.id = songId;
    this.albumId = albumId;
    this.data = songData;
    this.title = songName;
    this.artist = songArtist;
    this.album = songAlbum;

}

public Long getId(){
    return id;
}

public Long getAlbumId(){
    return albumId;
}

public String getData(){
    return data;
}


public String getTitle() {
    return title;
}


public String getArtist() {
    return artist;
}

public String getAlbum(){
    return album;
}

So this are my 2 classes, and i want to display the album art of all songs in an imageView. i'm trying it with Picasso to load it to imagevie but doesn't work, i'm doing something wrong with the path i think, i don't know how to get the path of an album. How can i solve this? Thanks,

1

There are 1 best solutions below

6
On

The problem with the code is on your path. You instantiated path = to the position which is a number. To get path it must be:

 String path = songsList.get(position).get(MediaStore.Audio.Albums.ALBUM_ART);

You could use Glide or Picasso to show your images.

Check it out. https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Glide-Library

https://github.com/square/picasso