-1
@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();
}
}
}
노래 생성자 클래스;모든 노래에서 앨범 아트를 가져 와서 imageView에 추가하려면 어떻게해야합니까?
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;
}
내 2 개의 클래스이며 모든 노래의 앨범 아트를 imageView에 표시하고 싶습니다. imagevie에로드하려고 Picasso와 함께 시도하고 있지만 작동하지 않습니다. 내가 생각하는 경로에서 잘못된 일을하고 있는데, 앨범 경로를 얻는 방법을 모르겠습니다. 어떻게 해결할 수 있습니까? 고마워,
https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Glide-Library 나는 그것을 시도하지만 ' t 작업을 didn를. Look at edit – Vince
나는 앨범 아트의 경로를 얻는 방법을 모른다. – Vince
답을 업데이트했습니다. 귀하의 경로는 올바른 경로가 아닌 번호입니다. –