Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MediaStore list all songs from specific path

I want to get all the songs from one specific path on the device. in a example i want to get all the songs from one specific folder "music" from path: "/mnt/sdcard/music/" , what i need to change in my code to able to achieve this? i have this method that get all the songs from the device:

public ArrayList<Song> scanAllSongsOnDevice(Context c)
    {
        ContentResolver musicResolver = c.getContentResolver();
        Uri musicUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        String col[] ={android.provider.MediaStore.Audio.Media._ID};
Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);


        if (musicCursor != null && musicCursor.moveToFirst())
        {
            // clear  list to prevent duplicates
            songsList = new ArrayList<>();

            //get columns
            int titleColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = musicCursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.ARTIST);
            int isMusicColumn = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.IS_MUSIC);
            int duration = musicCursor.getColumnIndex
                    (MediaStore.Audio.Media.DURATION);

            //add songs to list
            do
            {
                String filePath = musicCursor.getString(musicCursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                // check if the file is a music and the type is supported
                if (musicCursor.getInt(isMusicColumn) != 0 && filePath != null && (FileExtensionFilter.checkValidExtension(filePath)) && musicCursor.getInt(duration) > 0)
                {
                    int thisId = musicCursor.getInt(idColumn);
                    String thisTitle = musicCursor.getString(titleColumn);
                    String thisArtist = musicCursor.getString(artistColumn);
                    Song song = new Song();
                    song.setId(thisId);
                    if(!thisArtist.equals("<unknown>"))
                    {
                        song.setArtist(thisArtist);
                        song.setTitle(thisTitle);
                    }
                    else
                    {
                        song.setArtist("");
                        song.setTitle("");
                    }
                    song.setSongPath(filePath);
                    File file = new File(filePath);
                    song.setFileName(file.getName().substring(0, (file.getName().length() - 4)));
                    songsList.add(song);
                }
            }
            while (musicCursor.moveToNext());
        }
        else  // if we don't have any media in the folder that we selected set NO MEDIA
        {
            addNoSongs();
        }

        musicCursor.close();

        if(songsList.size() == 0)
        {
            addNoSongs();
        }

        Collections.sort(songsList, new Comparator<Song>()
        {
            @Override
            public int compare(Song song, Song song2)
            {
                int compare = song.getTitle().compareTo(song2.getTitle());
                return ((compare == 0) ? song.getArtist().compareTo(
                        song2.getArtist()) : compare);
            }
        });

        return songsList;
    }
like image 999
AlexTheLion Avatar asked Nov 21 '25 23:11

AlexTheLion


1 Answers

You need to modify your audioCursor like

audioCursor = audioResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null,MediaStore.Audio.Media.DATA + " like ? ",
            new String[] {"%YOUR_SPECIFIC_FOLDER_NAME%"},  null);

Hope it will help you...

Thanks

like image 150
kamil Avatar answered Nov 23 '25 14:11

kamil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!