ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.Media.DATA,
MediaStore.Images.ImageColumns.BUCKET_ID,
MediaStore.Images.Media.DATE_MODIFIED
// ,"count(*) as count" //这一句也是在夜神上有效
},
MediaStore.Images.Media.MIME_TYPE + "=? or " +
MediaStore.Images.Media.MIME_TYPE + "=? or " +
MediaStore.Images.Media.MIME_TYPE + "=? or " +
MediaStore.Images.Media.MIME_TYPE + "=?) "
+ "group by (" + MediaStore.Images.ImageColumns.BUCKET_ID ,
new String[]{"image/jpeg", "image/png", "image/jpg", "image/gif"},
MediaStore.Images.Media.DATE_MODIFIED + " asc"
//MediaStore.Images.Media.DATE_MODIFIED + " asc)" (夜神测试运行时最后面加一个括号)
);
前面的代码块在夜神模拟器上运行是没有问题的,我故意在最后面多添加一个括号,报了语法错误,下面是报错信息打印的查询语句
SELECT _data, bucket_id, date_modified
FROM images
WHERE (mime_type=? or mime_type=? or mime_type=? or mime_type=?)
group by (bucket_id)
ORDER BY date_modified asc)
去掉最后面多余的")"后是正确的语法
SELECT _data, bucket_id, date_modified
FROM images
WHERE (mime_type=? or mime_type=? or mime_type=? or mime_type=?)
group by (bucket_id)
ORDER BY date_modified asc
下面是报错打印的查询语句,多加了几个AND,还把group by也括进去了
SELECT _data, bucket_id, date_modified
FROM images
WHERE (
(is_pending=0)
AND (is_trashed=0)
AND (volume_name IN ( 'external_primary' ))
)
AND ((mime_type=? or mime_type=? or mime_type=? or mime_type=?) group by (bucket_id))
ORDER BY date_modified asc
差别如下
//夜神
WHERE (mime_type=? or mime_type=? or mime_type=? or mime_type=?)
group by (bucket_id)
//小米9
WHERE (
(is_pending=0)
AND (is_trashed=0)
AND (volume_name IN ( 'external_primary' ))
)
AND ((mime_type=? or mime_type=? or mime_type=? or mime_type=?) group by (bucket_id))
多加了几个AND,还把group by也括进去了
这个问题该怎么办???
继续摸索,期待答案,有答案会及时更新的
不分组查询了,查询完之后遍历 cursor获取文件夹列表的时候,加上去重操作
//获取所有图片集
@RequiresApi(api = Build.VERSION_CODES.Q)
private ArrayList<ImageFolder> getAlbums(Context context) {
ArrayList<ImageFolder> albums = new ArrayList<>();
albums.add(getNewestPhotos(context));
//利用ContentResolver查询数据库,找出所有包含图片的文件夹,保存到相册列表中
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.Media.DATA,
MediaStore.Images.ImageColumns.BUCKET_ID,
MediaStore.Images.Media.DATE_MODIFIED},
MediaStore.Images.Media.MIME_TYPE + "=? or " +
MediaStore.Images.Media.MIME_TYPE + "=? or " +
MediaStore.Images.Media.MIME_TYPE + "=? or " +
MediaStore.Images.Media.MIME_TYPE + "=?",
new String[]{"image/jpeg", "image/png", "image/jpg", "image/gif"},
MediaStore.Images.Media.DATE_MODIFIED + " asc"
);
if (cursor != null) {
List<String> dirs = new ArrayList<>();
A:
while (cursor.moveToNext()) {
final File file = new File(cursor.getString(0));
for (String parent : dirs) {
if (parent.equalsIgnoreCase(file.getParent())) {
continue A;
}
}
ImageFolder imageFolder = new ImageFolder();
imageFolder.setDir(file.getParent());
imageFolder.setId(cursor.getString(1));
imageFolder.setFirstImagePath(cursor.getString(0));
dirs.add(file.getParent());
String[] all = file.getParentFile().list(new FilenameFilter() {
private boolean e(String filename, String ends) {
return filename.toLowerCase().endsWith(ends);
}
@Override
public boolean accept(File dir, String filename) {
return e(filename, ".png") || e(filename, ".jpg") || e(filename, "jpeg") || e(filename, ".gif");
}
});
if (all != null && all.length > 0) {
imageFolder.setCount(all.length);
albums.add(imageFolder);
}
}
Log.e("test", dirs.toString());
cursor.close();
}
return albums;
}