Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android list files contained in assets subfolder

I create some folders into assets. Each folder contains files that I would like to list. I am using following code but I always get a null value for fileList. Thank you.

I used, listFiles("/assets/images/","nothing");

private void listFiles(String dirFrom, String dirTo) {


        File f = new File(dirFrom);

        String fileList[] = f.list();

            if (fileList != null)
            {   
                for ( int i = 0;i<fileList.length;i++)
                {
                    Log.d("",fileList[i]); 
                }
            }
    }    
like image 524
Jaume Avatar asked Mar 05 '12 10:03

Jaume


People also ask

What does asset folder contain?

The Assets folder is where you should save or copy files that you want to use in your project.

Where is the Android Assets folder?

In Android Studio, click on the app folder, then the src folder, and then the main folder. Inside the main folder you can add the assets folder.

What is Android Assets folder?

Assets provide a way to add arbitrary files like text, XML, HTML, fonts, music, and video in the application. If one tries to add these files as “resources“, Android will treat them into its resource system and you will be unable to get the raw data. If one wants to access data untouched, Assets are one way to do it.


2 Answers

You'll probably want to do this:

private void listFiles(String dirFrom) {
        Resources res = getResources(); //if you are in an activity
        AssetManager am = res.getAssets();
        String fileList[] = am.list(dirFrom);

            if (fileList != null)
            {   
                for ( int i = 0;i<fileList.length;i++)
                {
                    Log.d("",fileList[i]); 
                }
            }
    }

Also your function call should be: listFiles("images"); if you want to list images.

like image 99
user Avatar answered Sep 27 '22 17:09

user


Simplest would surely be this:

String[] fileList = getAssets().list("images");
like image 28
Mike Redrobe Avatar answered Sep 27 '22 18:09

Mike Redrobe