Search code examples
javaandroiddirectoryexternalmediastore

Android Java - Read music from specific folder


I'm following a tutorial and have modified it to come up with this:

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

    String[] proj = { MediaStore.Audio.Media._ID,
            MediaStore.Audio.Media.DATA, 
            MediaStore.Audio.Media.DISPLAY_NAME};

    Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    String sortOrder = MediaStore.Audio.Media.DISPLAY_NAME + " ASC";

    musiccursor = getContentResolver().query(uri, proj, selection, null, sortOrder);

The above code will read all the music on my external. Is there a way for me to specify a specific a folder to read music from? I'm guessing I'll have to change the uri since that is pointing to my external, I think??


Solution

  • Yes you can do by

    musiccursor = getContentResolver().query( uri,
                    proj,
                    MediaStore.Audio.Media.DATA + " like ? ",
                    new String[] {"%myfolder%"},
                    sortOrder);