Search code examples
androidpathmp3filefilter

how to catch all songs in sdcard and also sdcard folders like music and etc


I use this code to find mp3 files:

`

   // String extStore = System.getenv("EXTERNAL_STORAGE");
   // File home = new File(extStore);


    //String extStore = "/storage/extSdCarcd";
    String extStore = "/storage/";
    File home = new File(extStore);

    if(home.listFiles(new FileExtensionFilter()).length>0){

        for(File file : home.listFiles(new FileExtensionFilter())){

            HashMap<String,String> song = new HashMap<String, String>();
            song.put("title",file.getName().substring(0,(file.getName().length()-4)));
            song.put("path",file.getPath());
            songsList.add(song);
        }
    }

    return songsList;
}`

as you see,I tried many ways to get .mp3 files,but if I have an mp3 file in my bluetooth folder or music folder,they won't help. they are just for music in sdcard


Solution

  • for getting all mp3 songs you have to use media store db which will list down the all songs of the device

    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
    
    
    public ArrayList<HashMap<String, String>> getPlayList(Context c) {
    
        final Cursor mCursor = c.getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaColumns.TITLE, MediaColumns.DATA, AudioColumns.ALBUM }, null, null,
            "LOWER(" + MediaColumns.TITLE + ") ASC");
    
        String songTitle = "";
        String songPath = "";
    
        /* run through all the columns we got back and save the data we need into the arraylist for our listview*/
        if (mCursor.moveToFirst()) {
            do {
                songTitle = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.TITLE));
                songPath = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaColumns.DATA));
    
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", songTitle);
                song.put("songPath", songPath);
                songsList.add(song);
    
            } while (mCursor.moveToNext());
        }   
    
        mCursor.close(); //cursor has been consumed so close it
        return songsList;
    }