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]);
}
}
}
The Assets folder is where you should save or copy files that you want to use in your project.
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.
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.
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.
Simplest would surely be this:
String[] fileList = getAssets().list("images");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With