0

I have a series of folders within my asset folder. Each folder contains pictures and documents. I'm having trouble getting an activity to list the contents of my asset folder. I've tried a number of solutions that I've found online but can't get anything to work. In this activity I'm trying to just list the folders within the asset folder. From there a user could click on a folder and open it. I think I have a decent understanding of how to open the folder when clicked. I'm just struggling with how to list the folders.

I have tried: Adding this to my onCreate method:

    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

    final AssetManager assetManager = getAssets();
    try {
        // for assets folder add empty string
        String[] filelist = assetManager.list("");
        // for assets/subFolderInAssets add only subfolder name

        if (filelist == null) {
            // dir does not exist or is not a directory
        } else {
            for (int i=0; i<filelist.length; i++) {
                // Get filename of file or directory
                String filename = filelist[i];
            }
        }
        } catch (IOException e) {
        e.printStackTrace();
    } }

Nothing shows up when I do this.

I have tried this:

public static  List<String> listAssetFiles(Context c, String rootPath) {
    List<String> files =new ArrayList<>();
    try {
        String [] Paths = c.getAssets().list(rootPath);
        if (Paths.length > 0) {
            // This is a folder
            for (String file : Paths) {
                String path = rootPath + "/" + file;
                if (new File(path).isDirectory())
                    files.addAll(listAssetFiles(c,path));
                else files.add(path);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return files;
}

I've tried a few other things as well. I cannot seem to make this work. Any help would be much appreciated. Thanks in advance.

  • You want to list the files recursively. Check one of the suggestions in https://stackoverflow.com/questions/2056221/recursively-list-files-in-java – Queeg May 03 '22 at 05:14
  • Okay, thank you. I will try that. After that though, I don't see how it will cause it to be displayed in the activity. I'm trying to use recyclerview because I need it to update at runtime when I search for items. – RogueBotanist May 03 '22 at 15:09

0 Answers0