Storage Folders to Adapter
friostd opened this issue · 11 comments
How do I get all the storage folders and add them to the treeViewAdapter? I've tried all sorts of ways, but I can't.
File root = new File("/storage/emulated/0/");
for (File file : root.listFiles()) {
TreeNode node = new TreeNode(file.getName(), R.layout.layout);
}
The problem is adding the child folders, I don't know how to do this.
list.add(node);
treeViewAdapter.updateTreeNodes(list);
Hello @FrioGitHub
Your code will only get the first level of folders for example if you run it with path of D0, you will get D1 and D2 Only
D0
D1
- F1
- F2
- F3
D2
- F4
- F5
- F6
To get all storge folders and files you need to create a file crawler for example
public TreeNode crawlerStorageFiles(File parentPath) {
if (parentPath.isDirectory()) {
TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
for (File file : parentPath.listFiles()) {
node.addChild(crawlerStorageFiles(file));
}
return node;
} else {
TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
return node;
}
}
This code will first get Start from D0 and find D1 if it files it will return it and if it directory it will create a node and recursion on it to get all of children
and on the end, you will add it to the list of roots and add it to the adapter
TreeNode root = crawlerStorageFiles(new File("/storage/emulated/0/"));
list.add(node);
treeViewAdapter.updateTreeNodes(list);
Hello @FrioGitHub
Your code will only get the first level of folders for example if you run it with path of D0, you will get D1 and D2 Only
D0 D1 - F1 - F2 - F3 D2 - F4 - F5 - F6
To get all storge folders and files you need to create a file crawler for example
public TreeNode crawlerStorageFiles(File parentPath) { if (parentPath.isDirectory()) { TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout); for (File file : parentPath.listFiles()) { node.addChild(crawlerStorageFiles(file)); } return node; } else { TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout); return node; } }This code will first get Start from D0 and find D1 if it files it will return it and if it directory it will create a node and recursion on it to get all of children
and on the end, you will add it to the list of roots and add it to the adapter
TreeNode root = crawlerStorageFiles(new File("/storage/emulated/0/")); list.add(node); treeViewAdapter.updateTreeNodes(list);
bro, it doesn't work with files. it only shows folders
Hello @FrioGitHub,
If you passed a path for one file it will return it as TreeNode,
If you have one folder with many files you should pass the path of this folder not a single file to get all of them
@AmrDeveloper I tried it but Iam getting nullPointerException fails to get …getLayoutId() on a null object reference
@myusersnamesis One of your nodes is null, try to add check in the crawler function to check if you pass null
@AmrDeveloper like this?
public TreeNode crawlerStorageFiles(File parentPath) {
if (parentPath.isDirectory()) {
TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
If (!parentPath == null){
for (File file : parentPath.listFiles()) {
node.addChild(crawlerStorageFiles(file));
}
}
return node;
} else {
TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
return node;
}
}
Try to check if crawlerStorageFiles
return null in any stage for example
public TreeNode crawlerStorageFiles(File parentPath) {
if (parentPath.isDirectory()) {
TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
If (!parentPath == null){
for (File file : parentPath.listFiles()) {
TreeNode n = crawlerStorageFiles(file);
if (n == null) {
Log.d(TAG, "Node is null);
}
node.addChild(n);
}
}
return node;
} else {
TreeNode node = new TreeNode(parentPath.getName(), R.layout.layout);
return node;
}
}
@AmrDeveloper Thank you it worked. Another question can I get files located in storage/emulated/0/ as root like if I have:
0
downloads
- music.mp3
- image.png
- treeview-master
android
- data
- on
- obj
I need to get downloads
and android
as roots not 0.
You're welcome bro,
If you have directory 0
the straightforward solution is to do this
// This will loop on downloads, android...etc
for (File file : zeroDirectory.listFiles()) {
TreeNode zeroSubFile = crawlerStorageFiles(file);
}
This code will give you a list of zero sub roots which are what you want [downloads, android] with their children's
How to implement feature for inserting new item and remove existing item(delete/creat new file/folder) without effecting treeview state for some following methods
and so on...