public class MainActivity extends Activity implements OnClickListener {
// 显示图片的视图
private ImageView iv;
// 路径输入的输入框
private EditText etPath;
// 关键词输入框
private EditText etKw;
// 记录加载出来的文件
private File[] files;
// 记录当前显示的文件在数组中的下标(下一张以及上一张的运算的参考)
private int index;
// 显示当前浏览进度
private TextView tvTips,tvTipsPath;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
etPath = (EditText) findViewById(R.id.et_path);
// 给输入框设置默认值
etPath.setText("/mnt/sdcard");
tvTips = (TextView) findViewById(R.id.tv_tips);
etKw = (EditText) findViewById(R.id.et_kw);
findViewById(R.id.btn_load).setOnClickListener(this);
findViewById(R.id.btn_pre).setOnClickListener(this);
findViewById(R.id.btn_next).setOnClickListener(this);
// 搜索按钮设置监听
findViewById(R.id.btn_search).setOnClickListener(this);
tvTipsPath = (TextView) findViewById(R.id.tv_tips_path);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_load:
// 加载数据
// 从输入框中获取用户输入的路径
String path = etPath.getText().toString();
// 如果路径不为空
if (!"".equals(path.trim())) {
// 加载数据
files = getImages(path);
index = 0;
// 显示默认的第1张
if (null != files && files.length > 0) {
showImage();
} else {
tvTips.setText("暂无图片");
}
} else {
tvTips.setText("路径不能为空");
}
break;
case R.id.btn_pre:
// 显示上一张
if (null != files) {
int len = files.length;
// 如果是第一张,则上一张设置为最后一张的下标,否则将下标-1移到前一个
if (index == 0) {
index = len - 1;
} else {
index--;
}
showImage();
}
break;
case R.id.btn_next:
// 显示下一张
if (null != files) {
int len = files.length;
if (index == len - 1) {
index = 0;
} else {
index++;
}
showImage();
}
break;
case R.id.btn_search:
// 搜索用户输入的文件夹及其子文件夹中名字包含关键字输入框中的内容的图片
// 从输入框中获取用户输入的路径
String folderPath = etPath.getText().toString();
// 如果路径不为空
if (!"".equals(folderPath.trim())) {
String kw = etKw.getText().toString();
if (!"".equals(kw.trim())) {
// 加载数据
files = searchFiles(folderPath, kw);
index = 0;
// 显示默认的第1张
if (null != files && files.length > 0) {
showImage();
} else {
tvTips.setText("暂无图片");
}
} else {
tvTips.setText("关键词不能为空");
}
} else {
tvTips.setText("路径不能为空");
}
break;
}
}
//根据当前下标显示图片
private void showImage(){
File f = files[index];
iv.setImageURI(Uri.fromFile(f));
tvTips.setText((index + 1) + "/" + files.length); // 0 6
tvTipsPath.setText(f.getPath());
}
/**
* 搜索文件
*
* @param folderPath
* 文件夹路径
* @param keyWord
* 关键词
* @return
*/
private File[] searchFiles(String folderPath, String keyWord) {
// 记录当前遍历的队列
LinkedList<File> fileQueue = new LinkedList<File>();
// 记录最终搜索的文件
LinkedList<File> resultFiles = new LinkedList<File>();
// 预设要搜索的根文件夹就是第一个要遍历的目标
fileQueue.addLast(new File(folderPath));
while (!fileQueue.isEmpty()) {
// 获取队列首的文件夹来遍历其中的孩子
File folder = fileQueue.removeFirst();
File[] fs = folder.listFiles();
if (fs != null) {
// 检测文件夹中孩子是否有满足条件的文件,或者还有子文件夹需要遍历
int len = fs.length;
for (int i = 0; i < len; i++) {
File f = fs[i];
if (f.isDirectory()) {
// 是文件夹,说明还需要去遍历才知道其中是否有满足条件的文件
// 记录到队列中,便于后面的遍历
fileQueue.addLast(f);
} else {
// 是不是我们要的文件
if (f.getName().contains(keyWord)) {
// 如果名字中包含该关键词,则再检测一下是否是图片
if (imageFilter.accept(f)) {
// 找到了满足条件的文件
resultFiles.add(f);
}
}
}
}
}
}
// 将结果转为数组
if (!resultFiles.isEmpty()) {
File[] files = new File[resultFiles.size()];
resultFiles.toArray(files);
return files;
}
return null;
}
/**
* 加载图片
*
* @param folderPath
* @return
*/
private File[] getImages(String folderPath) {
// 创建文件夹对象
File folder = new File(folderPath);
if (folder.isDirectory()) {
File[] fs = folder.listFiles(imageFilter);
return fs;
}
return null;
}
// 加载图片的过滤器
private FileFilter imageFilter = new FileFilter() {
@Override
public boolean accept(File file) {
String name = file.getName();
return name.endsWith(".jpg") || name.endsWith(".jpeg")
|| name.endsWith(".png");
}
};
}
文件遍历
最新推荐文章于 2022-01-21 00:48:54 发布