【问题标题】:Android get all folders that contain at least one media file like photo / videoAndroid 获取包含至少一个媒体文件(如照片/视频)的所有文件夹
【发布时间】:2021-07-05 13:24:34
【问题描述】:

我只需要包含至少一个以例如结尾的文件的文件夹路径。 .mp4 或 .jpg 。我已经检查了这个thread: Android: how to get all folders with photos?,但它对我的用例来说太慢了。有更快的方法吗?

【问题讨论】:

    标签: android gallery


    【解决方案1】:

    所以我终于找到了一个比上面提到的更快的解决方案。我在 ~100-150 毫秒(大约 3k 个文件)内获得了所有带有图像的目录。在下面的示例中,将检查存储在内部或外部存储中的所有图像文件,并将父目录添加到数组列表中。我排除了 .gif 和 .gif 文件。 此方法也适用于视频,因此需要几个步骤:

    • queryUri 更改为 MediaStore.Video.Media.EXTERNAL_CONTENT_URI
    • 投影更改为MediaStore.Video.Media.DATA
    • includeImages 中的 'image/%' 更改为 'vid​​eo/%'
    • 选择中删除+ excludeGif

    public static ArrayList<String> getImageDirectories(Context mContext) {
            ArrayList<String> directories = new ArrayList<>();
    
            ContentResolver contentResolver = mContext.getContentResolver();
            Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    
    
            String[] projection = new String[]{
                    MediaStore.Images.Media.DATA
            };
    
            String includeImages = MediaStore.Images.Media.MIME_TYPE + " LIKE 'image/%' ";
            String excludeGif =  " AND " + MediaStore.Images.Media.MIME_TYPE + " != 'image/gif' " + " AND " + MediaStore.Images.Media.MIME_TYPE + " != 'image/giff' ";
            String selection =  includeImages + excludeGif;
    
            Cursor cursor = contentResolver.query(queryUri, projection, selection, null, null);
    
            if (cursor != null && cursor.moveToFirst()) {
                do {
    
                    String photoUri = cursor.getString(cursor.getColumnIndex(projection[0]));
                    if(!directories.contains(new File(photoUri).getParent())){
                        directories.add(new File(photoUri).getParent());
                    }
    
                } while (cursor.moveToNext());
            }
    
            return directories;
        }
    

    【讨论】:

      猜你喜欢
      • 2014-07-04
      • 2020-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-31
      • 1970-01-01
      • 2011-12-18
      • 1970-01-01
      相关资源
      最近更新 更多