-1

How can I know the number of images inside a particular file in assets What should I use?

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
Taha Sami
  • 1,565
  • 1
  • 16
  • 43

2 Answers2

1

One line code

Int no_of_imgs = getResources().getAssets().list("foldar_name").length;

This works inside activity or fragment class.

Mayank Kumar Chaudhari
  • 16,027
  • 10
  • 55
  • 122
1

this code count android assets folder all files also containing in subfolders

private int countAssetFiles(String path) {
    int filesCount = 0;
    String [] list;
    try {
        list = getAssets().list(path);
        if (list.length > 0) {  
            for (String file : list) {
                // if there is a subfolder this line add count of that subfolder to  total count else add 1(one file)
                filesCount += listAssetFiles(path + "/" + file);
            }
        } 
    } catch (IOException e) {
         //path is a file
         return 1;
    }

    return filesCount; 
}

by below code you can get all files count in asset folder

int allFilesCount = countAssetFiles("");

if you want to get certain folder files in asset use this

int filesInSubFolder = countAssetFiles("/subFolderName");
Mehdi Varse
  • 271
  • 1
  • 15