how to check a html file exist in assets folder

815 Views Asked by At
Assets
 |-man
 |  |-myhtml.html
 |
 |-women
 |  |-myhtml.html

this is my folder structure some times i need to check the file exist in men some times i need to check the file exist in women what can i do.

try {
        fileExist = Arrays.asList(getResources().getAssets().list("")).contains(pathInAssets);
    } catch (FileNotFoundException e) {
        fileExist = false;
    } catch (IOException e) {
        e.printStackTrace();
    }

this give only the list man and women i cant go inside it and check the existance.

Is their any other way to check this

2

There are 2 best solutions below

0
On

Just open an input stream for the file. As if you were going to read the file.

If you manage to open it the file does exist. Otherwise you get an exception.

0
On

use below method to get file from assets folder. i provide code get all .mp4 file and store file into string list.

private List<String> videoList=new ArrayList<>();
  private boolean listAssetFiles(String path) {

    String [] list;
    try {
        list = getAssets().list(path);
        if (list.length > 0) {
            // This is a folder
            for (String file : list) {
                if (!listAssetFiles(path + "/" + file))
                    return false;
                else {
                    // This is a file
                    // TODO: add file name to an array list
                    if (file.contains(".mp4") || file.contains(".mp3") ) {
                        videoList.add(file);
                    }
                }
            }
        }
    } catch (IOException e) {
        return false;
    }

    return true;
}

when you call this method pass only ..

    listAssetFiles(""); // you can pass your path if is empty then it scan root.