可以获得文件夹中所有文件的路径及文件名。
代码很简单,直接上车,车上再解释:
/** * 获取指定目录内所有文件路径 * @param dirpath 需要查询的文件目录 * @param _type 查询类型,比如mp3什么的 */ public static jsonarray getallfiles(string dirpath, string _type) { file f = new file(dirpath); if (!f.exists()) {//判断路径是否存在 return null; } file[] files = f.listfiles(); if(files==null){//判断权限 return null; } jsonarray filelist = new jsonarray(); for (file _file : files) {//遍历目录 if(_file.isfile() && _file.getname().endswith(_type)){ string _name=_file.getname(); string filepath = _file.getabsolutepath();//获取文件路径 string filename = _file.getname().substring(0,_name.length()-4);//获取文件名 // log.d("logcat","filename:"+filename); // log.d("logcat","filepath:"+filepath); try { jsonobject _finfo = new jsonobject(); _finfo.put("name", filename); _finfo.put("path", filepath); filelist.put(_finfo); }catch (exception e){ } } else if(_file.isdirectory()){//查询子目录 getallfiles(_file.getabsolutepath(), _type); } else{ } } return filelist; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
LongClick原理、上下文菜单原理、EditText长按弹窗原理、WebView长按弹窗自定义、修复WebView全选重复bug ———————————————— 版权声明:本文为CSDN博主「
JobScheduler 实现 特定时间,特定条件(系统空闲,电池电量,磁盘空间 ……)下执行任务
网友评论