遍历SD卡,获取含有图片文件的文件夹集合

来源:互联网 发布:qq飞车改装30雷诺数据 编辑:程序博客网 时间:2024/06/07 20:32



今天遇到这样的需求:得到SD中含有图片文件的文件夹,并以列表方式显示出来,效果如上图所示,网上找了一遍,没发现有人共享这样的代码,所以只好自己写了。


思路很简单:直接扫描sd卡所有文件夹,找出所有包含后缀为.bmp,.jpg,.jpeg,png等的文件的文件夹。

完整代码如下:

<span style="white-space:pre"></span>/** * 得到包含有图片的文件夹list *  * 这个过程有点久,建议放到程序的启动界面进行 *  * @param context * @return */public static List<PicFolderBean> getPicFolderList(Context context) {// 包含有图片的文件夹路径listList<PicFolderBean> picFolderList = new ArrayList<PicFolderBean>();// SD卡根目录File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath());// 填充picFolderList数据fitPicFolderList(picFolderList, file);// 把填充好的picFolderList返回出去return picFolderList;}/** * 填充picFolderList数据 *  * @param picFolderList * @param file */private static void fitPicFolderList(List<PicFolderBean> picFolderList,File file) {System.err.println("fitPicFolderList方法,当前目录是:" + file.getAbsolutePath());// 文件夹的名字String folderName = "";// 文件夹路径String folderPath = "";// 文件夹中第一张图片的地址String firstPicPath = "";// 文件夹中图片的数量int picCount = 0;// 得到文件夹里的所有文件/文件夹对象File[] listFiles = file.listFiles();for (File f : listFiles) {// 递归// 如果是文件夹if (f.isDirectory()) {System.err.println("是文件夹进入递归。。。");// 再执行自己这个函数,把picFolderList交给它去填充fitPicFolderList(picFolderList, f);}// 如果是文件if (isPic(f)) {System.err.println("是图片文件:" + f.getAbsolutePath());// 第一次发现这个文件夹中有图片,给folderName赋值if (TextUtils.isEmpty(folderName)) {folderName = f.getParentFile().getName();}// 第一次发现这个文件夹中有图片,给folderPath赋值if (TextUtils.isEmpty(folderPath)) {folderPath = f.getParentFile().getAbsolutePath();}// 第一次发现这个文件夹中有图片,给firstPicPath赋值if (TextUtils.isEmpty(firstPicPath)) {firstPicPath = f.getAbsolutePath();}picCount++;}}// 如果有数量,说明有图片if (picCount > 0) {picFolderList.add(new PicFolderBean(firstPicPath, folderName,folderPath, picCount));}}/** * 判断是不是图片 *  * @param f * @return */private static boolean isPic(File f) {// TODO Auto-generated method stubString path = f.getAbsolutePath();// 得到文件的后缀名int lastIndex = path.lastIndexOf(".");if (lastIndex < 0) {// 没有格式return false;}// lastIndex + 1得到的是png,如果不加1则得到.pngString format = path.substring(lastIndex + 1, path.length());System.out.println("当前文件格式为:" + format.toLowerCase());// 如果是图片格式就返回truefor (String tmpFormat : picFormat) {if (tmpFormat.equals(format.toLowerCase())) {System.err.println("是图片");return true;}}return false;}

以下是表示一个含有图片的文件夹的信息类(bean)

package com.lqr.utils;/** * 表示一个含有图片的文件夹的信息 *  * @author Administrator *  */public class PicFolderBean {// 第一张图片地址public String firstPicPath = "";// 文件夹的名字public String folderName = "";// 文件夹路径public String folderPath = "";// 文件夹里的图片数量public int picCount;public String getFirstPicPath() {return firstPicPath;}public void setFirstPicPath(String firstPicPath) {this.firstPicPath = firstPicPath;}public String getFolderName() {return folderName;}public void setFolderName(String folderName) {this.folderName = folderName;}public String getFolderPath() {return folderPath;}public void setFolderPath(String folderPath) {this.folderPath = folderPath;}public int getPicCount() {return picCount;}public void setPicCount(int picCount) {this.picCount = picCount;}public PicFolderBean(String firstPicPath, String folderName,String folderPath, int picCount) {super();this.firstPicPath = firstPicPath;this.folderName = folderName;this.folderPath = folderPath;this.picCount = picCount;}@Overridepublic String toString() {return "PicFolderBean [firstPicPath=" + firstPicPath + ", folderName="+ folderName + ", folderPath=" + folderPath + ", picCount="+ picCount + "]";}}




0 0
原创粉丝点击