我是android新手。我正在使用服务实现音乐播放器。以下是错误集中的代码。任何人都可以帮我解决这个错误吗?
public void getSong() {
ContentResolver contentResolver = getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor cursor = contentResolver.query(musicUri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
long thisId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID));
String thisTitle = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String thisArtist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int albumId = cursor.getColumnIndex(android.provider.MediaStore.Audio.Albums.ALBUM_ART);
String thisArt = cursor.getString(albumId);
musicArrayList.add(new Music(thisId, thisTitle, thisArtist, thisArt));
} while (cursor.moveToNext());
}
}
答案 0 :(得分:0)
确保用于获取具有相同列名的数据的表 看看这个http://www.rjkfw.com/android/s3035.html
答案 1 :(得分:0)
在提取数据之前检查null。
ListView audioListView = (ListView) findViewById(R.id.songView);
ArrayList<string> audioList = new ArrayList<>();
String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DISPLAY_NAME };// Can include more data for more details and check it.
Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
if(audioCursor != null){
if(audioCursor.moveToFirst()){
do{
int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
audioList.add(audioCursor.getString(audioIndex));
}while(audioCursor.moveToNext());
}
}
audioCursor.close();
ArrayAdapter<string> adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,android.R.id.text1, audioList);
audioListView.setAdapter(adapter);
答案 2 :(得分:0)
错误解决,因为我正在寻找特定歌曲的专辑,所以在游标中传递空值。所以我改变了我的光标代码如下:
public void getSong() {
ContentResolver contentResolver = getContentResolver();
Cursor cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.ALBUM},
MediaStore.Audio.Media.IS_MUSIC + "=1",
null,
null);
if (cursor != null && cursor.moveToFirst()) {
do {
long thisId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
String thisTitle = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String thisArtist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String thisImage = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
songList.add(new Music(thisId, thisTitle, thisArtist, thisImage));
}while (cursor.moveToNext());
}
我尝试在服务类的playSong()中调用getsongImage(URI)方法来获取Album,如下所示:
public void getSongImage(Uri uri) {
MediaMetadataRetriever mmr = new MediaMetadataRetriever();
mmr.setDataSource(this,uri);
byte [] data = mmr.getEmbeddedPicture();
if(data==null){
activitySongDetailBinding.imgMusic.setImageResource(R.drawable.img);
}else {
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
activitySongDetailBinding.imgMusic.setImageBitmap(bitmap);
}
}
其中uri = ContentUris.withAppendedId( android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,songsArrayList.get(songPosn).getId())