I have a huge list
of Song
objects. Each object holds 4 Strings
(title, album, artist, path) and an integer
(album_id to get the album art later). However, I need to pass a part of this list or even the whole list
to a new activity
which will play these songs.
But yeah, you're right! That's a lot of memory! I reduced it by passing only the paths and in the onCreate()
method of the new activity I will read all songs on the device and add them only to the playlist if the paths match. This still takes time and maybe more memory than it should take.
What can I do to reduce memory usage and time to get the list
of songs from one activity to another?
If the approach to pass a list of paths to the new activity and read the files (directly) by its path was a good idea, how do I do that? So far I have this code but it's inefficient. It takes as argument a list of paths to files we want in our playlist and reads all songs on the external storage. then it'll check each song if its path is inside the pathlist, if not it'll continue.
public static List<Song> getSongList(List<String> pathList, Context c) {
Cursor audioCursor = c.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
new String[] { "*" }, null, null, Media.TITLE + " ASC");
ArrayList<Song> songsList = new ArrayList<Song>();
if (audioCursor != null) {
if (audioCursor.moveToFirst()) {
do {
String path = audioCursor.getString(audioCursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
if( !pathList.contains(path) ){
//if it's not in the list, we don't want it!
continue;
}
String title = audioCursor.getString(audioCursor
.getColumnIndex(MediaStore.Audio.Media.TITLE));
String album = audioCursor.getString(audioCursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
String artist = audioCursor.getString(audioCursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int album_id = audioCursor.getInt(audioCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Song s = new Song();
s.setPath(path);
s.setTitle(title);
s.setArtist(artist);
s.setAlbum(album);
s.setAlbumId(album_id);
songsList.add(s);
} while (audioCursor.moveToNext());
}
}
// return songs list array
return songsList;
}