遍历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
- 遍历SD卡,获取含有图片文件的文件夹集合
- Android加载SD卡目录,文件夹遍历,图片设置,设置文件对应打开方式等
- Android 遍历删除SD卡中某一文件夹下的指定子文件夹及其子文件
- 遍历SD卡或者具体文件夹下面具体的文件的实现
- android怎么获取一个SD卡指定文件夹的图片Uri uri有关问题
- 获取android sd卡中某个文件夹下的所有图片
- 获取android sd卡中某个文件夹下的所有图片
- 获取SD卡上所有的图片
- 遍历文件夹内所有文本文件,查找含有某种模式的行,合并到一个文件
- java-获取.csv文件里的数据,并且获取文件夹下所含有对象的个数
- 保存图片到sd卡指定的(创建)文件夹
- Android 读取SD卡中相应文件夹下的图片
- 遍历文件夹的文件
- 遍历工程中的文件夹的文件或者图片
- python遍历文件夹获取指定格式的文件
- 将图片存入本地SD卡文件夹
- QT实现遍历文件夹图片文件
- 遍历文件夹,操作XML文件,读取图片
- 读《股票操盘手记》手机版
- 开源大数据查询分析引擎现状
- 今天开通博客,记录一下IOS学习历程
- 深入理解Java 虚拟机(周志明)笔记(二)——Java数据分区
- Linux 可加载内核模块剖析 (2.6 内核透视图)
- 遍历SD卡,获取含有图片文件的文件夹集合
- codeforces #345 div.2 A Joysticks
- 深入理解Java 虚拟机(周志明)笔记(三)——Java对象在内存中的存储与访问
- jQuery源码分析系列(30) : Ajax 整体结构
- PyQt4入门教程(6)_对话框
- 企业会计学
- 新手融入linux项目开发的必过关------编译工程代码
- 如何给maven项目增加新的jar包(从服务器上下载)---通过eclipse工具
- 深入理解Java 虚拟机(周志明)笔记(四)——Java对象创建过程