Search code examples
androidandroid-contentresolvermediastore

MediaStore to list from specific path


I am trying to get list of mp3 files from a particular folder.

As of now, I am using contentResolver.query for getting the music from my phone.

Like this:

String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";

        String[] projection = {
                MediaStore.Audio.Media._ID,
                MediaStore.Audio.Media.ARTIST,
                MediaStore.Audio.Media.TITLE,
                MediaStore.Audio.Media.DATA,
                MediaStore.Audio.Media.DISPLAY_NAME,
                MediaStore.Audio.Media.DURATION
        };

        ContentResolver contentResolver = context.getContentResolver();
        cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,projection, selection,null,null);

I am looking for solution to scan only a particular folder. How do I do this using contentResolver?

Thanks!


Solution

  • Finally figured it out myself. Hope it will help others.

    Instead of this:

    cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null);
    

    I used this:

    cursor = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection,MediaStore.Audio.Media.DATA + " like ? ",
                    new String[] {"%MyMusicFolder%"},  null);