【问题标题】:Count the Images form sd card [closed]计算 SD 卡中的图像 [关闭]
【发布时间】:2014-09-16 08:08:05
【问题描述】:

我创建了一个 android 应用程序来计算 SD 卡图像并在 textview 中显示计数。我试过但没有结果..请解决我的问题..或给出任何其他想法..

这是我的代码

            File dir = new File(Environment.getExternalStorageDirectory()
            + "/");
    File[] files = dir.listFiles();
    int numberOfImages=files.length;
         textView1= (TextView ) findViewById(R.id.textView1);
         textView1.setText(numberOfImages);

}}

【问题讨论】:

标签: android image count sd-card


【解决方案1】:

您必须递归调用 sd 卡根文件夹并遍历每个文件夹并提取图像文件将文件添加到数组列表或数组中。试试下面的代码。

    public static ArrayList<File> getFolderList(File f, ArrayList<File> dir) {
    if (f == null) {
        return dir;
    } else
        try {
            if (f.isDirectory()) {
                File[] listFile = f.listFiles();
                if (listFile != null) {
                    fileCount = listFile.length;
                    for (int i = 0; i < fileCount; i++) {
                        if (listFile[i].isDirectory()) {
                            dir = getFolderList(listFile[i], dir);

                        } else {
                                  if (fName.endsWith(".png")
                                    || fName.endsWith(".jpeg")
                                    || fName.endsWith(".jpg")
                                    || fName.endsWith(".bmp")) {

                                dir.add(listFile[i]);

                            }



                        }
                    }
                }

            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    return dir;
}

参数:

文件 f :将 f 作为 sdcard 根文件夹路径或您想要获取计数的任何文件夹路径传递

ArrayList dir : 传递一个空的arraylist 来获取文件列表。如果您只想要文件数,则更改函数的返回类型并传递任何计算文件数的参数。

【讨论】:

  • 感谢您的回复
【解决方案2】:
public static final List<String> FILE_FORMATS_IMAGE = Arrays.asList("jpg","png");
int imageCount=0;
for (File file : dirs) {

             String ext = file.getAbsolutePath().toLowerCase().substring(file.getAbsolutePath().length() - 3);

            if (file.isDirectory()) {
                directories.add(new FileDetail(file.getName(),file.getAbsolutePath(),FileExplorerUtil.FLAG_DIRECTORY_ICON));
            } else {
                if(FILE_FORMATS_IMAGE.contains(ext.toLowerCase())){
               imageCount++;
                }

            }
        }

imageCount 的值表示 sdcard 中的图像总数。

【讨论】:

    【解决方案3】:
    public static final List<String> FILE_FORMATS_IMAGE = Arrays.asList("jpg","png");
    int imageCount=0;
    for (File file : dirs) {
    
                 String ext = file.getAbsolutePath().toLowerCase().substring(file.getAbsolutePath().length() - 3);
    
                if (file.isDirectory()) {
                //if want to count directory count here
                } else {
                    if(FILE_FORMATS_IMAGE.contains(ext.toLowerCase())){
                   imageCount++;
                    }
    
                }
            }
    

    imageCount 的值表示 sdcard 中的图像总数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-22
      • 2017-07-11
      • 1970-01-01
      相关资源
      最近更新 更多