Android: How to detect a directory in the assets folder?

前端 未结 9 1321
无人及你
无人及你 2021-01-05 18:15

I\'m retrieving files like this

String[] files = assetFiles.list(\"EngagiaDroid\"); 

How can we know whether it is a file or is a director

9条回答
  •  没有蜡笔的小新
    2021-01-05 18:55

    In your particular case, since you retrieved the files through list, you already know that these names exist. This simplifies the problem a lot. You can simply use this:

    public static boolean isAssetAFolder(AssetManager assetManager, String assetPath) throws IOException {
    
        // Attempt opening as a file,
        try {
            InputStream inputStream = assetManager.open(assetPath); inputStream.close();
            return false; // A file indeed.
        } catch (FileNotFoundException e) {
            // We already know this name exists. This is a folder.
            return true;
        }
    }
    

    On the other hand, if you need a generic solution to detect if a certain path both exists and is a folder, you can use this:

    public static boolean isAssetAFolder(AssetManager assetManager, String assetPath) throws IOException {
    
        // Attempt opening as a file,
        try {
            InputStream inputStream = assetManager.open(assetPath); inputStream.close();
            return false; // A file indeed.
        } catch (FileNotFoundException e) {
            // This could be a folder, or this path doesn't exist at all. Further checking needed,
            return assetPathExists(assetManager, assetPath);
        }
    }
    
    // If you are checking a file name "icon.png" inside your assets folder, the assetPath should be "icon.png".
    public static boolean assetPathExists(AssetManager assetManager, String assetPath) throws IOException {
    
        // Assume that "" exists by default,
        if (assetPath.isEmpty()) return true;
    
        // Reject paths that point outside the assets folder,
        if (assetPath.startsWith("..") || assetPath.startsWith("/")) return false;
    
        // For other file/folder paths, we'll search the parent folder,
        File fileOrFolder = new File(assetPath);
        String parent = ((parent=fileOrFolder.getParent()) != null) ? parent : ""; // Handle null parents.
        if (!Arrays.asList(assetManager.list(parent)).contains(fileOrFolder.getName())) return false;
    
        // Getting this far means that the specified assetPath indeed exists. However, we didn't handle files
        // with trailing "/". For instance, "icon.png/" shouldn't be considered existing although "icon.png"
        // does.
    
        // If the path doesn't end with a "/", we are safe,
        if (!assetPath.endsWith("/")) return true;
    
        // Remove the trailing slash,
        assetPath = assetPath.substring(0, assetPath.length()-1);
    
        // Attempt opening as a file,
        try {
            InputStream inputStream = assetManager.open(assetPath); inputStream.close();
            return false; // It's indeed a file (like "icon.png"). "icon.png/" shouldn't exist.
        } catch (FileNotFoundException e) {
            return true; // This is a folder that exists.
        }
    }
    

    I wrote these for a web server, so I couldn't make assumptions about the shape of the input path. But it can be simplified a bit if you have some rules set. This code returns immediately once it becomes certain of the type of the asset, to avoid the extra processing overhead.

提交回复
热议问题