Home » Android » android – List all music in MediaStore with the PATHs

android – List all music in MediaStore with the PATHs

Posted by: admin April 23, 2020 Leave a comment

Questions:

Ok so I’ve been working on this project for a few days now and most of my time has been working out how to list all the music on a device in a LIST VIEW or something else, I have searched for a few days now and this is killing me. I did get so close at one point with all the music in one folder showing, though since most people will have sub folders for things like artiest and albums I need a way to search sub folders for MP3s or music files.

Here is what I have so far for Music collection:

package com.androidhive.musicplayer;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.HashMap;

import android.provider.MediaStore;

public class SongsManager {
// SDCard Path
final String MEDIA_PATH = new String(MediaStore.Audio.Media.getContentUri("external").toString());
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

// Constructor
public SongsManager(){

}

/**
 * Function to read all mp3 files from sdcard
 * and store the details in ArrayList
 * */
public ArrayList<HashMap<String, String>> getPlayList(){
    File home = new File(MEDIA_PATH);

    if (home.listFiles(new FileExtensionFilter()).length > 0) {
        for (File file : home.listFiles(new FileExtensionFilter())) {
            HashMap<String, String> song = new HashMap<String, String>();
            song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
            song.put("songPath", file.getPath());

            // Adding each song to SongList
            songsList.add(song);
        }
    }
    // return songs list array
    return songsList;
}

/**
 * Class to filter files which are having .mp3 extension
 * */
class FileExtensionFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".mp3") || name.endsWith(".MP3"));
    }
}

}

Thanks to anyone who can help. 🙂

How to&Answers:

Although, the post is old, for other people like me to get the idea of creating a list of music with their file path, I added the solution here. MediaStore.Audio.Media.DATA column actually contains media file path. You can get necessary information by using the following snippet:

ContentResolver cr = getActivity().getContentResolver();

Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + "!= 0";
String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
Cursor cur = cr.query(uri, null, selection, null, sortOrder);
int count = 0;

if(cur != null)
{
    count = cur.getCount();

    if(count > 0)
    {
        while(cur.moveToNext())
        {
            String data = cur.getString(cur.getColumnIndex(MediaStore.Audio.Media.DATA));
            // Add code to get more column here

            // Save to your list here
        }

    }

    cur.close();
}

Answer:

You can list all the music files using this code

//Some audio may be explicitly marked as not being music
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
};

cursor = this.managedQuery(
        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
        projection,
        selection,
        null,
        null);

private List<String> songs = new ArrayList<String>();
while(cursor.moveToNext()) {
        songs.add(cursor.getString(0) + "||" 
                    + cursor.getString(1) + "||" 
                    + cursor.getString(2) + "||"
                    + cursor.getString(3) + "||"
                    + cursor.getString(4) + "||" 
                    + cursor.getString(5));
}

I have not tried this code, but it seems correct. You’ll be on the right track with that.

Answer:

I’m working on same project right now and already solved the problem.

You will need a custom class to store your songs data:

package YOUR_PACKAGE;

public class Songs
{
    private long mSongID;
    private String mSongTitle;

    public Songs(long id, String title){
        mSongID = id;
        mSongTitle = title;
    }

    public long getSongID(){
        return mSongID;
    }

    public String getSongTitle(){
        return mSongTitle;
    }
}

Then you have to define ArrayList in activity with List View which you will populate with data:

private ArrayList<Songs> arrayList;

and in onCreate:

arrayList = new ArrayList<Songs>();

Then you have to retrieve data from your device:

public void YOUR_METHOD_NAME(){
    ContentResolver contentResolver = getContentResolver();
    Uri songUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
    Cursor songCursor = contentResolver.query(songUri, null, null, null, null);

    if(songCursor != null && songCursor.moveToFirst())
    {
        int songId = songCursor.getColumnIndex(MediaStore.Audio.Media._ID);
        int songTitle = songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE);

        do {
            long currentId = songCursor.getLong(songId);
            String currentTitle = songCursor.getString(songTitle);
            arrayList.add(new Songs(currentId, currentTitle, currentArtist));
        } while(songCursor.moveToNext());
    }
}

Then call this method from onCreate:

YOUR_METHOD_NAME();

And finally you have to create custom adapter class, define this adapter in onCreate (in activity with ListView) and set this adapter on your ListView object.

I see that it was asked 3 years ago and the problem I think already solved, but maybe it will be usefull for someone. Thanks.

Answer:

Here is a simple function who gives you all audio files in File Object.

 public static List<File> getAllAudios(Context c) {
    List<File> files = new ArrayList<>();
    String[] projection = { MediaStore.Audio.AudioColumns.DATA ,MediaStore.Audio.Media.DISPLAY_NAME};
    Cursor cursor = c.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
    try {
        cursor.moveToFirst();
        do{
            files.add((new File(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)))));
        }while(cursor.moveToNext());

        cursor.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return files;
}