如何从Android中的文件夹和子文件夹中选取所有文件?

发布于 2024-12-21 02:46:48 字数 350 浏览 2 评论 0原文

可能的重复:
在Java中递归列出文件

我在SD卡中创建了一个名为SlideShow的文件夹 在这个文件夹中,我创建了两个文件夹,即folder1和folder2。这两个文件夹进一步分为两个子文件夹。我正在将图库中的一些图像保存到这些子文件夹中。

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

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 ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┼── 2024-12-28 02:46:48

我认为您可以使用这样的递归函数:您可以通过

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

图像列表中的此示例寻求帮助,您拥有所有图像。

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.

你怎么敢 2024-12-28 02:46:48

尝试使用以下代码获取 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;
    }

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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文