2011-10-05 52 views
0

在我們的應用程序中,我們想要獲取一個特定文件夾的音樂文件。我們想要這樣做,以使用媒體商店。獲取一個目錄的音樂文件

所以我們有一個路徑/ 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; 
    } 

回答

0

我可以告訴大家,這是行不通的:

android.provider.MediaStore.Audio.Media.DATA.substring(pathDirectory.length()).contains("/") 

MediaStore.Audio.Media.DATA只包含數據庫列一個常數,對操作是錯誤的。

您可能會在these answers中找到您的解決方案。

0

同樣的問題打了我。如果刪除 -

MediaStore.Audio.Media.IS_MUSIC + " != 0 

然後,它的工作原理。

+0

那麼它會工作。但我不知道爲什麼... – robot3