我正在开发Android中的音乐播放器,但仍然在阅读MP3文件.
这是我读取所有mp3文件的代码.但它不会从设备中撤回任何文件(尽管我使用adb复制了一些文件).我也希望它使用相册,艺术家等列出请帮助.
final String MEDIA_PATH = Environment.getExternalStorageDirectory()+"";private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();// Constructorpublic 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) { if (home.Listfiles(new fileExtensionFilter())!=null) { 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")); }}
解决方法:
在这里我修改了你的getPlayList()方法.了解更多.
ArrayList<HashMap<String,String>> getPlayList(String rootPath) { ArrayList<HashMap<String,String>> fileList = new ArrayList<>(); try { file rootFolder = new file(rootPath); file[] files = rootFolder.Listfiles(); //here you will get NPE if directory doesn't contains any file,handle it like this. for (file file : files) { if (file.isDirectory()) { if (getPlayList(file.getabsolutePath()) != null) { fileList.addAll(getPlayList(file.getabsolutePath())); } else { break; } } else if (file.getname().endsWith(".mp3")) { HashMap<String, String> song = new HashMap<>(); song.put("file_path", file.getabsolutePath()); song.put("file_name", file.getname()); fileList.add(song); } } return fileList; } catch (Exception e) { return null; } }
你可以得到这样的歌曲名称和歌曲路径:
@OverrIDeprotected voID onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentVIEw(R.layout.main_activity);ArrayList<HashMap<String,String>> songList=getPlayList("/storage/sdcard1/"); if(songList!=null){ for(int i=0;i<songList.size();i++){ String filename=songList.get(i).get("file_name"); String filePath=songList.get(i).get("file_path"); //here you will get List of file name and file path that present in your device log.e("file details "," name ="+filename +" path = "+filePath); } } }
Note: use “/storage/sdcard1/” for reading files from sdCard and use Environment.getExternalStorageDirectory().getabsolutePath() for reading files from phone memory
希望这会帮助你.
总结以上是内存溢出为你收集整理的列出android中的所有Mp3文件全部内容,希望文章能够帮你解决列出android中的所有Mp3文件所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)