3

I need some help. I need to iterate through assets folder and find all files and subfolders and do something with them. For example . I have created folder "myicons" in assets folder. But this folder(myicons) include other folders which include icons. It looks like

/myicons/buttons/playbt.ico ,/myicons/buttons/stopbt.ico , /myicons/shapes/square.ico

and so on. So I need to iterate through every subfolder but it doesn't work. AssetsManager.list(path) returns only files . Please suggest how to solve the problem. For example if getFromAssets(getAsset(),"myicons");

void getFromAssets (AssetManager mgr, String path) {
        try {
            String list[] = mgr.list(path);
            if (list != null)
                for (int i=0; i<list.length; ++i)
                    {
                    File file = new File(list[i]);  
                    if (file.isDirectory()) {
                        Log.d("Assets:",list[i]+ "is Directory");
                    }
                    else {
                        Log.d("Assets:",list[i]+ "is File");
                    }

                    }
        } catch (IOException e) {
            Log.v("List error:", "can't list" + path);
        }

    }

The problem was that if I have my_icons folder and in this folder I have buttons folder it didn't recognize buttons folder. list method doesn't recognize EMPTY FOLDERS. Everything works if folder is not empty . Thx everyone for help. I will post code a bit later .

  • possible duplicate of [How can I get a directory listing of resources from my Android app?](http://stackoverflow.com/questions/1495585/how-can-i-get-a-directory-listing-of-resources-from-my-android-app) – ben75 Jul 02 '14 at 12:49
  • What you're looking for is called recursion, and this could really be considered a duplicate of any question that asks about iterating a directory and all of it's sub directories. – Leon Newswanger Jul 02 '14 at 12:51
  • 1
    I understand that I should use recursion,but IT doesn't list folders ,only files !!! – user3796197 Jul 02 '14 at 12:55
  • Are you positive you're passing it the correct file paths? I really don't see anything wrong with your code itself. – Leon Newswanger Jul 02 '14 at 13:18
  • 1
    You cannot use class File for assets and hence you cannot use File.isDirectory(). Instead you have to make your own isDirectory(). You are wrong that folders are not listed. They get listed as well only you now don't know if it is a folder or file. Code to list all folders and files from assets has been published on this site before. Just search. Code for your own isDirectory too. Just search. – greenapps Jul 02 '14 at 13:34
  • 1
    After doing some research, greenapps is correct, this is the incorrect way to use assets. A workaround I have found is to call `mgr.list(list[i])` and check the length. If it contains files, it most certainly has to be a directory. So try changing you're if statement to `if (mgr.list(list[i]).length == 0)` – Leon Newswanger Jul 02 '14 at 13:40
  • Thank you for answers , I will try this. I have solved the problem . I have add it in my post. THX A LOT !! – user3796197 Jul 02 '14 at 14:31
  • If you've solved your issue, even if you used suggestions from the comments, please post what you did as an answer. It's okay to answer your own question and accept your own answer when there aren't any answers or the answers provided didn't help you. – Leon Newswanger Jul 03 '14 at 00:14

0 Answers0