【问题标题】:Scoped Storage android 10.0范围存储 android 10.0
【发布时间】:2021-01-02 21:08:27
【问题描述】:

崩溃在这里......

android.database.sqlite.SQLiteException:靠近“GROUP”:语法错误(Sqlite 代码 1 SQLITE_ERROR):,编译时:SELECT _id,bucket_display_name,bucket_id,_id,来自图像的方向 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,(操作系统错误 - 2:没有这样的文件或目录) 在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:184) 在 android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140) 在 android.content.ContentProviderProxy.query(ContentProviderNative.java:423) 在 android.content.ContentResolver.query(ContentResolver.java:955) 在 android.content.ContentResolver.query(ContentResolver.java:891) 在 android.content.ContentResolver.query(ContentResolver.java:840)

以下是显示所有文件夹或相册的方法。

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;
    }

下面是崩溃线......

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");

【问题讨论】:

  • 这与范围存储有什么关系?什么都没有!
  • @blackapps Android 10.0 这个崩溃发生了。 android 9.0 可以正常使用上面的代码为什么??
  • 你必须找出答案。只要您不知道,您就不会知道它是否与范围存储有关。我很确定这与它无关。你最好为你的帖子找到一个更好的主题,那就是关于 sqlite。 Android 10 对媒体商店的 SELECT 查询进行了更改。

标签: java android scoped-storage


【解决方案1】:

只需像下面这样更改查询:

来自

 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");

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");

【讨论】:

  • 嗨,Bony,欢迎来到 StackOverflow。请将您的代码片段包含在代码块中以提高可读性。
【解决方案2】:

您的GROUP BY 出现在And 中会导致问题。请更改您的query 如下:

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

【讨论】:

  • 不清楚我是初学者。请更改上述查询以获得解决方案。
  • 我不知道您的要求,但您遇到的问题是基于 SQL 语法错误。我用我的答案纠正了语法错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 2021-06-03
相关资源
最近更新 更多