0

I made this code for getting file extensions array for checking if any images are in that folder:

       if (file.isDirectory()) {
              listFile = file.listFiles();

              extension = new String[listFile.length];
              for (int i = 0; i < listFile.length; i++) {
                    FilePathStrings[i] = listFile[i].getAbsolutePath();
                    FileNameStrings[i] = listFile[i].getName();
                    int ex = FilePathStrings[i].lastIndexOf('.');
                    if (ex >= 0) {
                        extension[i] = FilePathStrings[i].substring(ex+1);

                    }
              }
              if (!Arrays.asList(extension).contains("jpg") || !Arrays.asList(extension).contains("jpeg") 
                      || !Arrays.asList(extension).contains("png")) {

              //Do Something
              }

But it's always returns true even if there are some images with exact "jpg" extension in the path (the part that gets file path and listfile is 100% working).

I tried with both filename and filepath but non of them worked.

SaDeGH_F
  • 471
  • 1
  • 3
  • 14
  • 1
    There is nothing that is returned. And // Do something will never be executed because of !Array... || ! Array... || ! Array. You better place some log statements or print/log the contents of extension array. – greenapps Sep 25 '14 at 19:03
  • @greenapps I placed 'Log.d("ggg", extension[i]);' and perfectly printed extensions in logcat (and there was jpg also) but it's returned true again. – SaDeGH_F Sep 25 '14 at 19:18
  • Where is your "return true" code? – San Sep 25 '14 at 19:44

3 Answers3

2

listFiles takes also a FileFilter as paramter

File[] files = file.list(new FilenameFilter() {
    public boolean accept(File directory, String fileName) {
        return fileName.endsWith(".jpg") || fileName.endsWith(".png");
    }
});

you can use it to filter out the file that you are not interested in

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • your answer is correct too but other one was more complete. But I didn't find my own code problem though. – SaDeGH_F Sep 25 '14 at 19:37
0

I believe this has been answered previously in Java: Find .txt files in specified folder

The above thread is used to find the names with txt extension which could be easily modified to return jpeg/jpg or png

Community
  • 1
  • 1
ceprateek
  • 11
  • 4
0

You dont have return true code in the question. I assume that you are asking why //Do Something code is getting executed?

It is because of the OR conditions in the If the statements

if (!Arrays.asList(extension).contains("jpg") || !Arrays.asList(extension).contains("jpeg") 
                  || !Arrays.asList(extension).contains("png")) {

          //Do Something
          }

If you have only jpg files, first condition wont be satisfied. But second/third will satisfy and //Do Something will be executed.

You have to change the code something like shown below

         if (Arrays.asList(extension).contains("jpg") || Arrays.asList(extension).contains("jpeg") 
                  || Arrays.asList(extension).contains("png")) {

              //Dont do... 
          }else{
              //Do Something
          }
San
  • 5,567
  • 2
  • 26
  • 28