gpt4 book ai didi

android - 在 Android 上过滤目录中的文件

转载 作者:太空狗 更新时间:2023-10-29 15:29:55 28 4
gpt4 key购买 nike

在我的应用程序中,我从图库中的文件夹中获取图像并将其保存到数组列表中。现在我只想提取扩展名为 .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;
}

最佳答案

您可以使用 FilenameFilter过滤文件的界面。

改变你的代码行

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"));
}
});

关于android - 在 Android 上过滤目录中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14353090/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com