In our app we want to get the music-files of one specific folder. We want to do this, to use the media store.
So we have a path /mnt/sd/music of this dir we will need all the files and not of the subdirs.
We tried to make a query with a substring, but it doesnt work.
public ArrayList<Song> getAudioFilesOfDir(String pathDirectory)
{
ArrayList<Song> songs = new ArrayList<Song>();
//Some audio may be explicitly marked as not being music
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0 AND " + android.provider.MediaStore.Audio.Media.DATA.substring(pathDirectory.length()).contains("/") ;
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.TRACK,
MediaStore.Audio.Media.YEAR,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_KEY,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.TITLE_KEY,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.ARTIST
};
ContentResolver cr = CoreLib.Context().getContentResolver();
Cursor cursor = cr.query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
null,
MediaStore.Audio.Media._ID);
int _ID_Column = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
int DATA_Column = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
int TRACK_Column = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
int YEAR_Column = cursor.getColumnIndex(MediaStore.Audio.Media.YEAR);
int DURATION_Column = cursor.getColumnIndex(MediaStore.Audio.Media.D开发者_开发百科URATION);
int ALBUM_ID_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID);
int ALBUM_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM);
//int ALBUM_KEY_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_KEY);
//int TITLE_Column = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
//int TITLE_KEY_Column = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE_KEY);
//int ARTIST_ID_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID);
int ARTIST_Column = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
while(cursor.moveToNext())
{
Song song = new Song();
song.setTrackId(cursor.getInt(_ID_Column));
song.setPathAudioFile(cursor.getString(DATA_Column));
song.setTrackName(cursor.getString(TRACK_Column));
song.setYearTrack(cursor.getInt(YEAR_Column));
song.setDuration(cursor.getString(DURATION_Column));
song.setAlbumId(cursor.getInt(ALBUM_ID_Column));
song.setAlbumName(cursor.getString(ALBUM_Column));
song.setArtist(cursor.getString(ARTIST_Column));
songs.add(song);
}
return songs;
}
I can tell that this won't work:
android.provider.MediaStore.Audio.Media.DATA.substring(pathDirectory.length()).contains("/")
MediaStore.Audio.Media.DATA
just contains a constant for database column and operating on that is just wrong.
You might find your solution in these answers.
精彩评论