JonSkeet 2013-03-22 09:17 采纳率: 0%
浏览 2872
已采纳

在 android 目录中过滤文件

我从图像库文件夹中获取图像,并把它保存在一个数组列表中。现在我只想提取有.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;
    }
  • 写回答

3条回答 默认 最新

  • LiDaming_and 2013-03-26 09:11
    关注

    使用 Java String 类中的.endsWith()方法来检查文件路径中的 File Extension。
    Method:

    public boolean endsWith(String suffix)
    

    代码如下:

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM