Manojit Paul Manojit Paul - 3 months ago 27
Android Question

Mediastore Query Simplification

I am trying to get all the music files in a device using Mediastore.
I am retrieving all the files asynchronously but it takes 6-7 seconds to get all the song , so during that time user has to see a in-determinant progressbar .

I have used a query inside a query to retrieve the AlbumArt of each song

Is there a method or a better approach to deal with it

public ArrayList<SongList> getSongs() {
ContentResolver musicResolver = context.getContentResolver();
Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;

Cursor musicCursor = musicResolver.query(musicUri, new String[] {MediaStore.Audio.Media.TITLE,MediaStore.Audio.Media._ID
,MediaStore.Audio.Media.ARTIST,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.ALBUM,MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.COMPOSER,MediaStore.Audio.Media.YEAR,MediaStore.Audio.Media.TRACK
}, null, null, MediaStore.Audio.Media.TITLE);

if (musicCursor != null && musicCursor.moveToFirst()) {
//int value=0;
//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 pathColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.DATA);
int albumColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ALBUM);
int albumIDColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.ALBUM_ID);
int composerColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.COMPOSER);
int yearColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.YEAR);
int trackColumn = musicCursor.getColumnIndex
(MediaStore.Audio.Media.TRACK);
String thisAlbumArt = "";

//add songs to list
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
String thisArtist = musicCursor.getString(artistColumn);
String thisPath = musicCursor.getString(pathColumn);
String thisAlbum = musicCursor.getString(albumColumn);
long thisalbumID = musicCursor.getLong(albumIDColumn);
String thisComposer = musicCursor.getString(composerColumn);
int thisyear = musicCursor.getInt(yearColumn);
int track = musicCursor.getInt(trackColumn);

songQueryList.add(thisTitle);
Cursor cursor = musicResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
MediaStore.Audio.Albums._ID + "=?",
new String[]{musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))},
null);
if (cursor!=null && cursor.moveToFirst()) {

thisAlbumArt = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Albums.ALBUM_ART));
cursor.close();
}
if(thisPath.endsWith(".mp3")|| thisPath.endsWith(".m4p") || thisPath.endsWith(".wav") ) {

songs.add(new SongList(thisId, thisTitle, thisAlbum, thisArtist, thisPath, thisAlbumArt ,thisyear,track ,thisComposer ,thisalbumID));
}
else{

}
//cursor.moveToNext();
}
while (musicCursor.moveToNext());
}
if(musicCursor!=null) {
musicCursor.close();
}
//Log.e("HI"," "+musicCursor.isClosed());

/*Collections.sort(songs, new Comparator<SongList>(){
public int compare(SongList a, SongList b){
return a.getTitle().compareToIgnoreCase(b.getTitle());
}
});*/

return songs;

}


Thankyou in advanced

Answer

I found the solution.Actually this few lines of codes where causing the delay which i was calling inside a do-while loop-

Cursor cursor = musicResolver.query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI,
                    new String[]{MediaStore.Audio.Albums._ID, MediaStore.Audio.Albums.ALBUM_ART},
                    MediaStore.Audio.Albums._ID + "=?",
                    new String[]{musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID))},
                    null);

I want to add that querying for a cursor is an very expensive and time taking line so using it multiple time causes a delay.

Rather i used 3 different query for SONG,ALBUM and ARTIST and then used some logic to interrelate them.

Hope this helps. Happy Coding.