1

Possible Duplicate:
Recursively list files in Java

I have created one folder named SlideShow in sdcard. Inside this folder i created two folders namely folder1 and folder2. These two folders further divided into two subfolders each. I am saving some images from gallery into these subfolders.

Anyone suggest me how to list all the images from the SlideShow folder including the folders and subfolders ??

Community
  • 1
  • 1
Santhosh_pulliman
  • 2,119
  • 6
  • 30
  • 47

2 Answers2

3

I think that you can use recrusive function like this: you can take help through this example

public List<String> images=new ArrayList<String>();        

public recursiveFunction(File dirPath) {
    File f = new File(dirPath);
    File[] files = f.listFiles();

    for(int i=0;i,**files.length - 1**;i++)
    {
        if(files[position].isFile())
        {
          int mid= files[position].getName().lastIndexOf(".");
          String ext=files[position].getName().substring(mid+1,files[position].getName().length()); 

          if(   ext.equalsIgnoreCase("jpg")
             || ext.equalsIgnoreCase("png")
             || ext.equalsIgnoreCase("jpeg")
             || ext.equalsIgnoreCase("gif"))
          {
            images.add(files[position].getAbsoluteFile();
          }
        }
        else 
          recursiveFunction(files[position].getAbsoluteFile());
    }
  }

in images list you have your all images.

Rob Kielty
  • 7,958
  • 8
  • 39
  • 51
Hemant Menaria
  • 701
  • 1
  • 7
  • 17
1

Try with the following code to get all images from SlideShow folder and its sub folder

public static ArrayList<String> getPathOfAllImages(Activity activity) {
        trimCache();
        String absolutePathOfImage = null;
        String nameOfFile = null;
        String absolutePathOfFileWithoutFileName = null;
        Uri uri;
        Cursor cursor;
        int column_index;
        int column_displayname;
        int lastIndex;
        // absolutePathOfImages.clear();
        ArrayList<String> absolutePathOfImageList = new ArrayList<String>();
        if (absolutePathOfImageList.isEmpty()) {
            uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

            String[] projection = { MediaColumns.DATA,
                    MediaColumns.DISPLAY_NAME };

            cursor = activity.managedQuery(uri, projection, null, null, null);
            column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);

            column_displayname = cursor
                    .getColumnIndexOrThrow(MediaColumns.DISPLAY_NAME);

            // cursor.moveToFirst();
            while (cursor.moveToNext()) {
                // for(int i=0; i<cursor.getColumnCount();i++){
                // Log.i(TAG,cursor.getColumnName(i)+".....Data Present ...."+cursor.getString(i));
                // }
                // Log.i(TAG,"=====================================");

                absolutePathOfImage = cursor.getString(column_index);
                nameOfFile = cursor.getString(column_displayname);

                lastIndex = absolutePathOfImage.lastIndexOf(nameOfFile);

                lastIndex = lastIndex >= 0 ? lastIndex
                        : nameOfFile.length() - 1;

                absolutePathOfFileWithoutFileName = absolutePathOfImage
                        .substring(0, lastIndex);

                if (!((absolutePathOfFileWithoutFileName.equals(Environment
                        .getExternalStorageDirectory() + "/SlideShow/")))) {
                    if (absolutePathOfImage != null) {
                        absolutePathOfImageList.add(absolutePathOfImage);
                    }
                }
            }

        }
        // Log.i(TAG,"........Detected images for Grid....."
        // + absolutePathOfImageList);
        return absolutePathOfImageList;
    }
Sunil Kumar Sahoo
  • 53,011
  • 55
  • 178
  • 243