Hoàng Dũng Phi
Hoàng Dũng Phi

Reputation: 41

Getting all music files

I am trying to get all music files of all the songs present on the phone. I am using mediastore to fetch all the songs title,artist etc. How should I fetch all music files ? Activity class:

public void getSongList() {
        //query external audio
        ContentResolver musicResolver = getContentResolver();
        Uri musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Cursor musicCursor = musicResolver.query(musicUri, null, null, null, null);
        //iterate over results if valid
        if (musicCursor != null && musicCursor.moveToFirst()) {
            //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);
            //add songs to list
            do {
                long thisId = musicCursor.getLong(idColumn);
                String thisTitle = musicCursor.getString(titleColumn);
                String thisArtist = musicCursor.getString(artistColumn);
                songList.add(new Song(thisId, thisTitle, thisArtist));
            }
            while (musicCursor.moveToNext());
        }
    }

Upvotes: 2

Views: 2930

Answers (1)

Chirag Solanki
Chirag Solanki

Reputation: 1277

I am also having same problem just like you.Now I am solve This Problem just using following code.

   /**
 * It Not used AnyWhere in Project but it's Helpful while you need to get all mp3 file in sdcard.
 * it's nothing but just for future used
 *
 */
public void getAllSongs() {

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

    String[] STAR=null;
    Cursor cursor = getActivity().managedQuery(allsongsuri, STAR, selection, null, null);
    //or
    //Cursor cursor = getActivity().getContentResolver().query(allsongsuri, null, null, null, selection);




    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                String song_name = cursor
                        .getString(cursor
                                .getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                int song_id = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.Audio.Media._ID));

                String fullpath = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
                String Duration = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION));

                Log.e(TAG, "Song Name ::"+song_name+" Song Id :"+song_id+" fullpath ::"+fullpath+" Duration ::"+Duration);

            } while (cursor.moveToNext());

        }
        cursor.close();

    }
}

this Code is Working fine in My Application.In This Code we have just Print whole song path and it's name in Log.but you are too intelligent so you can change this code according to your Requirement.

I hope you are clear with my Idea. Best of Luck

Upvotes: 3

Related Questions