今天遇到这样的需求:得到SD中含有图片文件的文件夹,并以列表方式显示出来,效果如上图所示,网上找了一遍,没发现有人共享这样的代码,所以只好自己写了。
思路很简单:直接扫描sd卡所有文件夹,找出所有包含后缀为.bmp,.jpg,.jpeg,png等的文件的文件夹。
完整代码如下:
<span style="white-space:pre"> </span>/**
* 得到包含有图片的文件夹list
*
* 这个过程有点久,建议放到程序的启动界面进行
*
* @param context
* @return
*/
public static List<PicFolderBean> getPicFolderList(Context context) {
// 包含有图片的文件夹路径list
List<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 stub
String path = f.getAbsolutePath();
// 得到文件的后缀名
int lastIndex = path.lastIndexOf(".");
if (lastIndex < 0) {// 没有格式
return false;
}
// lastIndex + 1得到的是png,如果不加1则得到.png
String format = path.substring(lastIndex + 1, path.length());
System.out.println("当前文件格式为:" + format.toLowerCase());
// 如果是图片格式就返回true
for (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;
}
@Override
public String toString() {
return "PicFolderBean [firstPicPath=" + firstPicPath + ", folderName="
+ folderName + ", folderPath=" + folderPath + ", picCount="
+ picCount + "]";
}
}