【问题标题】:How to pick all files from folders and subfolders in Android? [duplicate]如何从 Android 中的文件夹和子文件夹中选择所有文件? [复制]
【发布时间】:2011-12-13 12:56:59
【问题描述】:

可能重复:
Recursively list files in Java

我在 sdcard 中创建了一个名为 SlideShow 的文件夹。在这个文件夹中,我创建了两个文件夹,即文件夹 1 和文件夹 2。这两个文件夹进一步分为两个子文件夹。我正在将画廊中的一些图像保存到这些子文件夹中。

有人建议我如何列出 SlideShow 文件夹中的所有图像,包括文件夹和子文件夹?

【问题讨论】:

    标签: android directory android-file


    【解决方案1】:

    我认为你可以像这样使用递归函数:你可以通过这个例子得到帮助

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

    在图片列表中,您拥有所有图片。

    【讨论】:

      【解决方案2】:

      尝试使用以下代码从 SlideShow 文件夹及其子文件夹中获取所有图像

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

      【讨论】:

      • 我只需要幻灯片文件夹中的文件,而不需要任何其他媒体文件。
      猜你喜欢
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2021-07-23
      • 2014-07-22
      • 2010-11-07
      • 2018-11-21
      • 1970-01-01
      • 2020-11-24
      相关资源
      最近更新 更多