我正在尝试制作一个MusicPlayer应用程序。我想使用内容解析器显示AlbumArt。
以下是我用于获取歌曲信息的ContentResolver。
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
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
int artistColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.ARTIST);
int albumart = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Albums.ALBUM_ART);
//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
int thisAlbumArt = musicCursor.getInt(albumart);
songList.add(new Song(thisId, thisTitle, thisArtist , thisAlbumArt));
}
while (musicCursor.moveToNext());
}
}
这是我的自定义getView方法,用于在Listview中显示视图(歌曲信息的图像和文本)。
@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());
albumArtView.setImageResource(currSong.getAlbumArt());
//set position as tag
songLay.setTag(position);
return songLay;
}
每当我尝试运行应用程序时,应用程序都会崩溃。
我只是想知道如何使用ContentResolver和上面提到的代码显示关于其歌曲的正确歌曲艺术。
谢谢。