Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage Folders to Adapter #4

Open
friostd opened this issue Jul 26, 2022 · 11 comments
Open

Storage Folders to Adapter #4

friostd opened this issue Jul 26, 2022 · 11 comments
Labels
question Further information is requested

Comments

@friostd
Copy link

friostd commented Jul 26, 2022

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 friostd closed this as completed Jul 26, 2022
@friostd friostd reopened this Jul 26, 2022
@friostd
Copy link
Author

friostd commented Jul 26, 2022

list.add(node);

treeViewAdapter.updateTreeNodes(list);

@AmrDeveloper
Copy link
Owner

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

@friostd
Copy link
Author

friostd commented Jul 27, 2022

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

@AmrDeveloper
Copy link
Owner

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

@myusersnamesis
Copy link

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

@AmrDeveloper
Copy link
Owner

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

@myusersnamesis
Copy link

myusersnamesis commented Sep 16, 2022

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

@AmrDeveloper
Copy link
Owner

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

@myusersnamesis
Copy link

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

@AmrDeveloper
Copy link
Owner

AmrDeveloper commented Sep 16, 2022

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 AmrDeveloper added the question Further information is requested label Feb 22, 2023
@kisonix
Copy link

kisonix commented Mar 26, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

4 participants