1

I created an android application to count the SD card images and displayed the no of count in textview.. I tried But No result ..Please solve My issue..or give Any other idea..

This Is My code

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

}}
timrau
  • 22,578
  • 4
  • 51
  • 64
Android
  • 35
  • 2
  • 7
  • possible duplicate of [Integer value in TextView](http://stackoverflow.com/questions/3994315/integer-value-in-textview) – timrau Sep 16 '14 at 08:18

3 Answers3

1

You have to recursively call sd card root folder and iterate through each folder and extract the image files add the file to an array list or array.Try below code.

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

Parameters :

File f : pass f as sdcard root folder path or any folder path you want to get the count

ArrayList dir : pass an empty arraylist to get the file list. If you want the file count only, then change the return type of the function and also pass any parameter that counts the number of file.

Sunny
  • 1,066
  • 1
  • 13
  • 32
0
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++;
                }

            }
        }

Value of imageCount is represent total no of image in sdcard.

DynamicMind
  • 4,240
  • 1
  • 26
  • 43
0
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++;
                }

            }
        }

Value of imageCount is represent total no of image in sdcard.

DynamicMind
  • 4,240
  • 1
  • 26
  • 43