Home > Mobile >  The Android ContentResolver. The problem of query grouping of the query
The Android ContentResolver. The problem of query grouping of the query

Time:09-23

as Android version upgrade, source code changes the

The sample code
 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 IMAGES_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)" (night god test running the back to add a brace)
);

The problem details
Night god Android5.1
In front of the block of code that run on night god simulator is no problem, I deliberately at the back to add a more parentheses, called the grammatical errors, the following error message is printed query
 SELECT _data while forming, bucket_id date_modified 
The FROM images'
The WHERE (mime_type=? The or mime_type=? The or mime_type=? The or mime_type=?)
Group by (bucket_id)
The ORDER BY date_modified asc)


Remove the back after extra ") "is the correct grammar
 SELECT _data while forming, bucket_id date_modified 
The FROM images'
The WHERE (mime_type=? The or mime_type=? The or mime_type=? The or mime_type=?)
Group by (bucket_id)
The ORDER BY date_modified asc



Question, run on millet 9 without charging a block of code error directly the
Here is an error to print the query, add a few AND, also not enclosed in the group by
 SELECT _data while forming, bucket_id date_modified 
The FROM images'
The WHERE (
(is_pending=0)
AND (is_trashed=0)
AND (volume_name IN (' external_primary))
)
AND ((mime_type=? The or mime_type=? The or mime_type=? The or mime_type=?) Group by (bucket_id))
The ORDER BY date_modified asc



Difference between the following
 
//night godThe WHERE (mime_type=? The or mime_type=? The or mime_type=? The or mime_type=?)
Group by (bucket_id)

//millet 9
The WHERE (
(is_pending=0)
AND (is_trashed=0)
AND (volume_name IN (' external_primary))
)
AND ((mime_type=? The or mime_type=? The or mime_type=? The or mime_type=?) Group by (bucket_id))


Have to be solved, bosses give directions


alternative
Not grouped query, query after traversal cursor when acquiring the folder list, add to heavy operation
//get all the photo collections 
@ RequiresApi (API=Build. VERSION_CODES. Q)
Private ArrayList GetAlbums Context (Context) {
ArrayList Albums=new ArrayList<> (a);
Albums. Add (getNewestPhotos (context));
//using ContentResolver query the database to find all contain pictures folder, save the album list
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 Dirs=new ArrayList<> (a);
A:
While (cursor. MoveToNext ()) {
Final File File=new File (cursor. Get string (0));
For (String parent: dirs) {
If (parent. EqualsIgnoreCase (file. The getParent ())) {
The continue A;
}
}
ImageFolder ImageFolder=new ImageFolder ();
ImageFolder. SetDir (file. The getParent ());
ImageFolder. SetId (cursor. Get string (1));
ImageFolder. SetFirstImagePath (cursor. Get string (0));
Dirs. Add (file. The getParent ());

String [] all=file. GetParentFile (). The list (new FilenameFilter () {

Private Boolean e (String filename, String ends) {
Return the filename. ToLowerCase (). The 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 & amp; & All the length & gt; 0 {
ImageFolder. SetCount (all. Length);
Albums. Add (imageFolder);
}
}
The e (" test ", dirs. ToString ());
Cursor. The close ();
}

Return albums;
}

  • Related