我如何从所有歌曲中获取专辑艺术并将其添加到ImageView中


@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();
    }
}
}

歌曲构造师类;

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;
}

所以这是我的两个班级,我想在图像视图中展示所有歌曲的专辑aart。我正在使用毕加索(Picasso)将其加载到ImageVie,但不起作用,我认为我认为的路径不错,我不知道如何获得专辑的道路。我该如何解决?谢谢,

代码的问题在您的路径上。您实例化路径=到一个数字的位置。要获得路径,必须是:

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

您可以使用Glide或Picasso显示您的图像。

检查一下。https://github.com/codepath/android_guides/wiki/displaying-images-with-the-the-glide-library

https://github.com/square/picasso

相关内容

  • 没有找到相关文章

最新更新