【问题标题】:How I can get the number of files inside the folder in assets? [duplicate]如何获取资产中文件夹内的文件数? [复制]
【发布时间】:2018-12-01 16:50:50
【问题描述】:

我如何知道资产中特定文件内的图像数量我应该使用什么?

【问题讨论】:

  • 一个特定的文件,还是一个特定的文件夹?
  • 没有区别我的项目中有一个文件,里面我想知道使用代码的图像数量
  • 里面的图片数量是多少?什么类型的文件包含多个图像?
  • 在 assets 文件夹内,例如 assets/Images/here all Images

标签: android


【解决方案1】:

一行代码

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

这适用于活动或片段类。

【讨论】:

    【解决方案2】:

    此代码计算 android assets 文件夹中的所有文件也包含在子文件夹中

    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; 
    }
    

    通过以下代码,您可以获得资产文件夹中的所有文件计数

    int allFilesCount = countAssetFiles("");
    

    如果您想获取资产中的某些文件夹文件,请使用此

    int filesInSubFolder = countAssetFiles("/subFolderName");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-10
      • 1970-01-01
      • 2011-06-25
      • 2015-08-01
      • 2011-08-07
      • 2012-01-14
      • 2014-05-16
      • 1970-01-01
      相关资源
      最近更新 更多