如何从所有歌曲中获取专辑封面并将其添加到imageView?

时间:2017-12-03 21:47:26

标签: android android-mediaplayer android-imageview android-cursorloader albumart

x = np.arange(8)
barPlot(x, df[("columnName1", 8)])

Song构造函数类;

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

}

所以这是我的2个课程,我想在imageView中显示所有歌曲的专辑封面。 我正在尝试与毕加索一起将它加载到imagevie但是不起作用,我正在做错的路径我认为,我不知道如何获得专辑的路径。 我怎么解决这个问题? 谢谢,

1 个答案:

答案 0 :(得分:0)

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

views.py

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

检查出来。 https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Glide-Library

https://github.com/square/picasso