在Android上的目录中过滤文件 [英] Filtering files in a directory on Android
本文介绍了在Android上的目录中过滤文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用中,我从图库中的文件夹中获取图像并将其保存到数组列表中.现在我只想提取扩展名为.jpg的文件.我该怎么做
保存到数组列表的代码是
私有列表< String>ReadSDCard(){//必须与SDCard中的目录匹配文件f =新文件("sdcard/data/crak");File [] files = f.listFiles();for(int i = 0; i< files.length; i ++){文件file = files [i];/*假定路径中的所有文件均为受支持的类型*/tFileList.add(file.getPath());}返回tFileList;}
解决方案
使用 Java字符串类中的 .endsWith()
方法从文件路径中检查文件扩展名./p>
方法:
公共布尔endsWith(带字符串后缀)
您的代码类似
私有列表< String>ReadSDCard(){//必须与SDCard中的目录匹配文件f =新文件("sdcard/data/crak");File [] files = f.listFiles();for(int i = 0; i< files.length; i ++){文件file = files [i];/*假定路径中的所有文件均为受支持的类型*/字符串filePath = file.getPath();if(filePath.endsWith(.jpg"))//检查.jpg文件扩展名的条件tFileList.add(filePath);}返回tFileList;}
In my app i am getting the images from a folder in gallery and saving it into an array list.Now i want to extract only the files with .jpg extension.How can i do it
The code for saving to array list is
private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCard
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
tFileList.add(file.getPath());
}
return tFileList;
}
解决方案
Use .endsWith()
method from Java String Class to check File Extension from file path.
Method:
public boolean endsWith(String suffix)
Your Code something like,
private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCard
File f = new File("sdcard/data/crak");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
File file = files[i];
/*It's assumed that all file in the path are in supported type*/
String filePath = file.getPath();
if(filePath.endsWith(".jpg")) // Condition to check .jpg file extension
tFileList.add(filePath);
}
return tFileList;
}
这篇关于在Android上的目录中过滤文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文