TreeView icon indicating copy to clipboard operation
TreeView copied to clipboard

Storage Folders to Adapter

Open friostd opened this issue 3 years ago • 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.

friostd avatar Jul 26 '22 02:07 friostd

list.add(node);

treeViewAdapter.updateTreeNodes(list);

friostd avatar Jul 26 '22 02:07 friostd

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);

AmrDeveloper avatar Jul 26 '22 08:07 AmrDeveloper

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

friostd avatar Jul 27 '22 19:07 friostd

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 avatar Jul 27 '22 20:07 AmrDeveloper

@AmrDeveloper I tried it but Iam getting nullPointerException fails to get …getLayoutId() on a null object reference

myusersnamesis avatar Sep 16 '22 09:09 myusersnamesis

@myusersnamesis One of your nodes is null, try to add check in the crawler function to check if you pass null

AmrDeveloper avatar Sep 16 '22 09:09 AmrDeveloper

@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;
    }
}

myusersnamesis avatar Sep 16 '22 10:09 myusersnamesis

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 avatar Sep 16 '22 12:09 AmrDeveloper

@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.

myusersnamesis avatar Sep 16 '22 14:09 myusersnamesis

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

AmrDeveloper avatar Sep 16 '22 18:09 AmrDeveloper

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

https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemInserted(int)

https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter#notifyItemRemoved(int)

and so on...

kisonix avatar Mar 26 '23 07:03 kisonix