Android は、指定されたディレクトリ内の指定されたファイル タイプのすべてのファイルのパスと名前を取得します。

JSONArray fileList = new JSONArray();//这个请放到全局

呼び出し方法

 getAllFiles("PPG",".txt");

メソッド固有

 /**
     * 获取指定目录内所有文件路径
     * @param dirPath 需要查询的文件目录
     * @param _type 查询类型,比如TXT什么的
     */
    public JSONArray getAllFiles(String dirPath, String _type) {

        File f = new File(String.valueOf(mWXSDKInstance.getContext().getExternalFilesDir(dirPath)));
        if (!f.exists()) {//判断路径是否存在
            return null;
        }
        File[] files = f.listFiles();
        if(files==null){//判断权限
            return null;
        }
        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);//获取文件名
                String fileName = _file.getName();//获取文件名
                Log.e("LOGCAT","fileName:"+fileName);
                Log.e("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;
    }

 

 

サンプル結果を上に示します。


————————————————
转载于:https://blog.csdn.net/weixin_43449246/article/details/125998618

おすすめ

転載: blog.csdn.net/weixin_42602900/article/details/133635271