在我的应用程序中,我从图库中的文件夹中获取图像并将其保存到数组列表中。现在我只想提取扩展名为 .jpg 的文件。我该怎么做
保存到数组列表的代码是
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;
}
您可以使用文件名过滤器 http://developer.android.com/reference/java/io/FilenameFilter.html过滤文件的界面。
改变你的代码线
File[] files=f.listFiles();
如下:
File[] jpgfiles = f.listFiles(new FileFilter() {
@Override
public boolean accept(File file)
{
return (file.getPath().endsWith(".jpg")||file.getPath().endsWith(".jpeg"));
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)