'How can I list the folders within my assets folder?
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
