List All Mp3 files in android

前端 未结 4 918
日久生厌
日久生厌 2021-02-03 16:28

I am developing music player in android but stuck in reading MP3 files. here is my code to read all mp3 files. but its not returing any files from device(although there are some

4条回答
  •  粉色の甜心
    2021-02-03 17:08

    Try This

    String path;
    File sdCardRoot = Environment.getExternalStorageDirectory();
    File dir = new File(sdCardRoot.getAbsolutePath() + "/yourDirectory/");
    
    if (dir.exists()) {
    
        if (dir.listFiles() != null) {
            for (File f : dir.listFiles()) {
                if (f.isFile())
                    path = f.getName();
    
                if (path.contains(".mp3")) {
                    yourArrayList.add(path);
    
                }
            }
        }
    }
    

提交回复
热议问题