0

Crash is here......

android.database.sqlite.SQLiteException: near "GROUP": syntax error (Sqlite code 1 SQLITE_ERROR): , while compiling: SELECT _id, bucket_display_name, bucket_id, _id, orientation FROM images WHERE ((is_pending=0) AND (is_trashed=0) AND (volume_name IN ( 'external_primary' ))) AND ((1) GROUP BY 1,(2)) ORDER BY date_modified DESC, (OS error - 2:No such file or directory) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:184) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140) at android.content.ContentProviderProxy.query(ContentProviderNative.java:423) at android.content.ContentResolver.query(ContentResolver.java:955) at android.content.ContentResolver.query(ContentResolver.java:891) at android.content.ContentResolver.query(ContentResolver.java:840)

Below is method to show all folders or album of images.

private boolean logGalleryFolders() {
        this.albumList = new ArrayList<>();
        List<Integer> bucketIdList = new ArrayList<>();
        Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");

        List<GridViewItem> items;
        int i;
        if (cur == null || !cur.moveToFirst()) {
            items = new ArrayList<>();
            for (i = 0; i < this.albumList.size(); i++) {
                items.add(new GridViewItem(this.activity, this.albumList.get(i).name, BuildConfig.FLAVOR + this.albumList.get(i).imageIdList.size(), true, this.albumList.get(i).imageIdForThumb, this.albumList.get(i).orientationList.get(0).intValue()));
            }
            this.albumList.add(new Album());
            this.albumList.get(this.albumList.size() - 1).gridItems = items;
            for (i = 0; i < this.albumList.size() - 1; i++) {
                this.albumList.get(i).gridItems = createGridItemsOnClick(i);
            }

            if (cur != null) {
                cur.close();
            }
            return true;
        }
        int bucketColumn = cur.getColumnIndex("bucket_display_name");
        int bucketId = cur.getColumnIndex("bucket_id");
        int imageId = cur.getColumnIndex("_id");
        int orientationColumnIndex = cur.getColumnIndex("orientation");
        do {
            Album album = new Album();
            int id = cur.getInt(bucketId);
            album.ID = id;
            if (bucketIdList.contains(id)) {
                Album albumFromList = this.albumList.get(bucketIdList.indexOf(album.ID));
                albumFromList.imageIdList.add(cur.getLong(imageId));
                albumFromList.orientationList.add(cur.getInt(orientationColumnIndex));
            } else {
                String bucket = cur.getString(bucketColumn);
                bucketIdList.add(id);
                album.name = bucket;
                album.imageIdForThumb = cur.getLong(imageId);
                album.imageIdList.add(album.imageIdForThumb);
                this.albumList.add(album);
                album.orientationList.add(cur.getInt(orientationColumnIndex));
            }
        } while (cur.moveToNext());
        items = new ArrayList<>();
        for (i = 0; i < this.albumList.size(); i++) {
            items.add(new GridViewItem(this.activity, this.albumList.get(i).name, BuildConfig.FLAVOR + this.albumList.get(i).imageIdList.size(), true, this.albumList.get(i).imageIdForThumb, this.albumList.get(i).orientationList.get(0).intValue()));
        }
        this.albumList.add(new Album());
        this.albumList.get(this.albumList.size() - 1).gridItems = items;
        for (i = 0; i < this.albumList.size() - 1; i++) {
            this.albumList.get(i).gridItems = createGridItemsOnClick(i);
        }
        cur.close();
        return true;
    }

Below is crash line......

Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");
Muhammad Ejaz
  • 43
  • 2
  • 9
  • What does this have to do with scoped storage? Nothing! – blackapps Sep 16 '20 at 05:18
  • @blackapps Android 10.0 this crash happen. android 9.0 working fine with the above code why?? – Muhammad Ejaz Sep 16 '20 at 05:32
  • That you have to find out. And as long as you dont know you will not know if it has anything to do with scoped storage. Im pretty shure it has nothing to do with it. You better find a better subject for your post which is about sqlite. There were changes in Android 10 concerning SELECT queries for the media store. – blackapps Sep 16 '20 at 05:39

2 Answers2

1

Just change the query like below:

From

 Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, "1) GROUP BY 1,(2", null, "date_modified DESC");

To

Cursor cur = this.context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{"_id", "bucket_display_name", "bucket_id", "_id", "orientation"}, null, null, "date_modified DESC");
Jobert
  • 1,532
  • 1
  • 13
  • 37
Bony Hinsu
  • 21
  • 2
  • Hi Bony and welcome to StackOverflow. Please enclose your code fragments into code blocks for better readability. – MathMax Jan 21 '21 at 10:22
0

Your GROUP BY comes inside the And makes the issue. Please change your query as below:

SELECT
  _id,
  bucket_display_name,
  bucket_id,
  _id,
  orientation
FROM
  images
WHERE
  (
    (is_pending = 0)
    AND (is_trashed = 0)
    AND (volume_name IN ('external_primary'))
  )
  AND ((1))
GROUP BY
  1,(2)
ORDER BY
  date_modified DESC
Shalu T D
  • 3,921
  • 2
  • 26
  • 37