-3

I have assest in which there are subfolder and files in this way ..

As show in image ..assest->www ->js or cs folder..then files

assest
 |
 |__www
     |
     |___js
     |    |
     |    |__a.js
     |
     |____cs
     |     |
     |     |__a.css
     |
     |____index.html

I need to print all files name ..can we print that files name

I make this function but not work ..

package com.mobilecem.atms;

import java.io.IOException;

import android.content.Context;
import android.util.Log;

public class GlobalFunction {
    public static boolean listAssetFiles(String path,Context context) {

        String [] list;
        try {


            list = context.getAssets().list(path);
            if (list.length > 0) {
                // This is a folder
                for (String file : list) {
                    if (!listAssetFiles(path + "/" + file, context))
                        return false;
                }
            } else {
                Log.d("File Name", "file present");
                // This is a file
                // TODO: add file name to an array list
        } 

        }catch (IOException e) {
            return false;
        }

        return true; 
    }

}

call like that

GlobalFunction.listAssetFiles("", aaa.this);

Result : list have : index.html , a.js, a css

Comment

Pallavi Sharma
  • 635
  • 2
  • 16
  • 45

2 Answers2

2

Here you go You will have all files and its sub folders details in files List enter image description here Check it out

List<String> files = new ArrayList<String>();

public boolean listAssetFiles(String path, Context context) {


    String[] list;
    try {


        list = context.getAssets().list(path);
        if (list.length > 0) {
            // This is a folder
            for (String file : list) {
                String root = TextUtils.isEmpty(path) ? "" : path + "/";
                String fullpath = root + file;
                files.add(fullpath);
                if (!listAssetFiles(root + file, context))
                    return false;
            }
        } else {

            // This is a file
            // TODO: add file name to an array list
        }

    } catch (IOException e) {
        return false;
    }

    return true;
}
Saleeh
  • 392
  • 4
  • 15
0

This is fully woriking and showing folder names ive just tested it

    private void listFiles(String dirFrom) throws IOException {
    Resources res = getResources(); // if you are in an activity
    AssetManager am = res.getAssets();
    String[] fileList = am.list(dirFrom);
    ArrayList<Drawable> dList = new ArrayList<>();

    if (fileList != null) {
        for (int i = 0; i < fileList.length; i++) {

            Log.d("FOLDER", fileList[i]);




    }
}

Call the listAssetFiles with the root folder name of your asset folder.

listAssetFiles("root_folder_name_in_assets");

If the root folder is the asset folder then call it with

    listAssetFiles("");
Umer Kiani
  • 3,783
  • 5
  • 36
  • 63