在我們的應用程序中,我們想要獲取一個特定文件夾的音樂文件。我們想要這樣做,以使用媒體商店。獲取一個目錄的音樂文件
所以我們有一個路徑/ mnt/sd /這個目錄的音樂,我們需要所有的文件而不是子目錄。
我們試圖用子串進行查詢,但它不起作用。
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.DURATION);
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;
}
那麼它會工作。但我不知道爲什麼... – robot3