获取一个目录的音乐文件

时间:2011-10-05 12:41:21

标签: java android

在我们的应用中,我们想要获取一个特定文件夹的音乐文件。我们想要这样做,使用媒体商店。

所以我们有一个这个目录的路径/ mnt / sd / music我们需要所有的文件而不是子目录。

我们尝试使用子字符串进行查询,但它不起作用。

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;
    }

1 个答案:

答案 0 :(得分:0)

我可以说这不起作用:

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

MediaStore.Audio.Media.DATA只包含数据库列的常量并且操作正确,这是错误的。

您可以在these answers找到解决方案。