Android 遍历文件夹中所有文件的实例代码
2019/7/7 20:36:58
本文主要是介绍Android 遍历文件夹中所有文件的实例代码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
可以获得文件夹中所有文件的路径及文件名。
代码很简单,直接上车,车上再解释:
/** * 获取指定目录内所有文件路径 * @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; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。
这篇关于Android 遍历文件夹中所有文件的实例代码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-01-18android.permission.read_media_video
- 2024-01-18android_getaddrinfo failed eai_nodata
- 2024-01-18androidmo
- 2024-01-15Android下三种离屏渲染技术
- 2024-01-09Android 蓝牙使用
- 2024-01-06Android对接华为AI - 文本识别
- 2023-11-15代码安全之代码混淆及加固(Android)
- 2023-11-10简述Android语音播报TTS
- 2023-11-06Android WiFi工具类
- 2023-07-22Android开发未来的出路