我想在Custom ArrayAdapter的帮助下在列表视图中显示带有歌曲名称的专辑封面。
我已经尝试了以下代码,歌曲名称和艺术家名称显示完美但不是专辑艺术。我尝试使用Bitmap解码图像但没有运气。
这是我的ListSong类,用于使用MediaStore获取歌曲。
package com.example.adityaverma.practicemusic;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Logger;
/**
* Created by Aditya Verma on 17-10-2017.
*/
public class ListSong extends Fragment {
private ArrayList<Song> songList;
private ListView songView;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.listsongs, container, false);
songView = (ListView) rootView.findViewById(R.id.songsList);
songList = new ArrayList<Song>();
getSongList();
SongAdapter songAdt = new SongAdapter(getActivity(), songList);
songView.setAdapter(songAdt);
return rootView;
}
//This method is used to retrieve the songs from external storage of the device and adding them into our listview.
public void getSongList() {
ContentResolver musicResolver = getActivity().getContentResolver();
Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
if (musicCursor != null && musicCursor.moveToFirst()) {
//get columns
int titleColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ARTIST);
int albumid = musicCursor.getInt
(musicCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(sArtworkUri, albumid);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(
context.getContentResolver(), albumArtUri);
bitmap = Bitmap.createScaledBitmap(bitmap, 30, 30, true);
} catch (FileNotFoundException exception) {
exception.printStackTrace();
bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.);
} catch (IOException e) {
e.printStackTrace();
}
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
long thisalbumid = musicCursor.getLong(albumid);
songList.add(new Song(thisId, thisTitle, thisArtist , thisalbumid));
}
while (musicCursor.moveToNext());
musicCursor.close();
}
}
}
这是我的bean类,用于存储SongInfo。
package com.example.adityaverma.practicemusic;
public class Song {
private long id;
private String title;
private String artist;
private long albumId;
public Song(long songID, String songTitle, String songArtist , long songalbumId) {
this.id = songID;
this.title = songTitle;
this.artist = songArtist;
this.albumId = songalbumId;
}
public long getID(){return id;}
public String getTitle(){return title;}
public String getArtist(){return artist;}
public long getAlbumId(){return albumId;}
}
最后这是我的SongAdapter,它用于将ListView与ListSong类绑定。这就是我认为的问题所在。
package com.example.adityaverma.practicemusic;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
public class SongAdapter extends BaseAdapter {
private ArrayList<Song> songs;
private LayoutInflater songInf;
public SongAdapter(Context c, ArrayList<Song> theSongs) {
songs = theSongs;
songInf = LayoutInflater.from(c);
}
@Override
public int getCount() {
return songs.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//map to song layout
LinearLayout songLay = (LinearLayout) songInf.inflate
(R.layout.songviewer, parent, 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 albumArtView = (ImageView) songLay.findViewById(R.id.albumArtSongs);
//get song using position
Song currSong = songs.get(position);
//get title and artist strings
songView.setText(currSong.getTitle());
artistView.setText(currSong.getArtist());
//set position as tag
songLay.setTag(position);
return songLay;
}
}
我只想用正确的歌曲名称和艺术家名称显示我的albumArt。
注意:我只是这方面的初学者,所以请详细说明你的答案,并指出我错在哪里。并且可能会给出解释。