Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering files in a directory on Android

Tags:

android

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;
    }
like image 258
Geethu Avatar asked Mar 11 '26 03:03

Geethu


1 Answers

You can use FilenameFilter interface to Filter the Files.

Change your codeline

File[] files=f.listFiles();

as below:

File[] jpgfiles = f.listFiles(new FileFilter() {
            @Override
            public boolean accept(File file)
            {
                return (file.getPath().endsWith(".jpg")||file.getPath().endsWith(".jpeg"));
            }
});
like image 160
TNR Avatar answered Mar 12 '26 16:03

TNR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!