Reputation: 129
I'm trying to find the list of tracks by album ids which are currently saved on the external memory. After getting the valid album ids I can fetch a list of tracks only if the album has more than 1 track. This is kinda weird by all albums with a single track is returning 0 cursor size where there should be at least one track. If the album has more than 1 tracks a list of tracks is receiving successfully. Here's the code snippet,
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.ARTIST_ID + "=" + id;
Cursor c = context.getContentResolver().query(
uri,
null,
MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " + selection,
null, MediaStore.Audio.Media.TITLE + " ASC");
ArrayList<MediaItem> listOfSongs = new ArrayList<MediaItem>();
c.moveToFirst();
while (c.moveToNext()) {
MediaItem songData = new MediaItem();
String title = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long duration = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
long albumId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
long artistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
String composer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.COMPOSER));
songData.setTitle(title);
songData.setAlbum(album);
songData.setArtist(artist);
songData.setDuration(duration);
songData.setPath(data);
songData.setAlbumId(albumId);
songData.setArtistId(artistId);
songData.setComposer(composer);
listOfSongs.add(songData);
}
c.close();
Log.d("SIZE", "SIZE: " + listOfSongs.size());
Upvotes: 3
Views: 547
Reputation: 11873
You've used the c.MoveToFirst()
right before running the while loop. As a result when the loop starts, the index will move to the next item instead of first. Check my modified version of the code,
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = "";
// album was requested
if (is_album) {
selection = MediaStore.Audio.Media.ALBUM_ID + "=" + id;
}
// artist was requested
else {
selection = MediaStore.Audio.Media.ARTIST_ID + "=" + id;
}
Cursor c = context.getContentResolver().query(
uri,
null,
MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " + selection,
null, MediaStore.Audio.Media.TITLE + " ASC");
ArrayList<MediaItem> listOfSongs = new ArrayList<MediaItem>();
if (c == null) {
// error - log some message
}
else if (c.getCount() < 1) {
// nothing to show - log some message
}
else {
while (c.moveToNext()) {
MediaItem songData = new MediaItem();
String title = c.getString(c.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ARTIST));
String album = c.getString(c.getColumnIndex(MediaStore.Audio.Media.ALBUM));
long duration = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.DURATION));
String data = c.getString(c.getColumnIndex(MediaStore.Audio.Media.DATA));
long albumId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
long artistId = c.getLong(c.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
String composer = c.getString(c.getColumnIndex(MediaStore.Audio.Media.COMPOSER));
songData.setTitle(title);
songData.setAlbum(album);
songData.setArtist(artist);
songData.setDuration(duration);
songData.setPath(data);
songData.setAlbumId(albumId);
songData.setArtistId(artistId);
songData.setComposer(composer);
listOfSongs.add(songData);
}
c.close();
Log.d("SIZE", "SIZE: " + listOfSongs.size());
}
Upvotes: 1