【发布时间】:2021-07-05 13:24:34
【问题描述】:
我只需要包含至少一个以例如结尾的文件的文件夹路径。 .mp4 或 .jpg 。我已经检查了这个thread: Android: how to get all folders with photos?,但它对我的用例来说太慢了。有更快的方法吗?
【问题讨论】:
我只需要包含至少一个以例如结尾的文件的文件夹路径。 .mp4 或 .jpg 。我已经检查了这个thread: Android: how to get all folders with photos?,但它对我的用例来说太慢了。有更快的方法吗?
【问题讨论】:
所以我终于找到了一个比上面提到的更快的解决方案。我在 ~100-150 毫秒(大约 3k 个文件)内获得了所有带有图像的目录。在下面的示例中,将检查存储在内部或外部存储中的所有图像文件,并将父目录添加到数组列表中。我排除了 .gif 和 .gif 文件。 此方法也适用于视频,因此需要几个步骤:
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;
}
【讨论】: