reiley
reiley

Reputation: 3761

Android: Scan music files from specific directories?

I want to my application to scan for music files only in those folders which I have specified in preferences. So that other music files like audio books do not get included.

Currently, I've written this code, that scans all music files:

private String[] getTrackList() {
        String[] result = null;
        String[] projection = { MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE };
        String selection = MediaStore.Audio.Media.IS_MUSIC + "!=" + 0;
        cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                projection, selection, null, MediaStore.Audio.Media.TITLE
                        + " ASC");
        result = new String[cursor.getCount()];
        int position = 0;
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            columnIndex = cursor
                    .getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
            result[position] = cursor.getString(columnIndex);
            position++;
        }
        return result;
    }

Upvotes: 2

Views: 2621

Answers (1)

Flynn81
Flynn81

Reputation: 4168

Check out the answer to a similar question (although it is for images, the same idea should apply to audio):

Get filename and path from URI from mediastore

Looks like DATA stores the file path. I think if you use the MediaStore.Audio.Media.DATA column as part of your where clause, you can return only audio files from the directory you want.

Upvotes: 1

Related Questions