private ArrayList<String> imgPath=new ArrayList<String>(); //定义一个数组用于保存文件路径
private static String[] imageFormat=new String[]{"jpg","bmp","gif"}; //定义图片格式
private boolean isImageFile(String path){ //判断是否为图片文件的方法
for (String format:imageFormat){
if (path.contains(format)){ //如果文件名字包含定义的格式后缀,则返回true
return true;
}
}
return false;
}
private void getSdCardImgFile(String url){ //获取指定路径下的指定格式图片文件,传入路径参数
File files=new File(url); //新定义一个文件,路径则为传入的url
File[] file=files.listFiles(); //遍历该文件所有的子文件夹生成文件夹数组
for (File f:file){ //for循环遍历到文件数组
if(f.isDirectory()){ //如果为文件夹,则递归调用此方法遍历子文件夹
getSdCardImgFile(f.getAbsolutePath()); //递归调用
}else {
if (isImageFile(f.getPath())){ //如果文件是图片文件
imgPath.add(f.getAbsolutePath());//获取绝对路径,返回到定义好的数组中。
}
}
}
}
递归调用的方式遍历SD卡上的所有图片
最新推荐文章于 2023-06-07 11:40:39 发布