Search code examples
androidandroid-contentresolverandroid-10.0

Invalid token SELECT


Hi I am trying to get the content of bucket Id using content resolver in android Q I am getting this Error

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #7
Process: com.dev.newtermain, PID: 13048
java.lang.IllegalArgumentException: Invalid token SELECT
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:172)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:142)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java:472)
    at android.content.ContentResolver.query(ContentResolver.java:1183)
    at android.content.ContentResolver.query(ContentResolver.java:1115)
    at android.content.ContentResolver.query(ContentResolver.java:1071)

My Selection query is

selection = "bucket_id = ?) UNION SELECT _data, date_added, 0 as isImage FROM video WHERE (bucket_id = ?";
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
selectionArgs = new String[]{bucketIdString};
String[] projection = new String[]{
            MediaStore.Images.ImageColumns.DATA, MediaStore.Images.ImageColumns.DATE_ADDED, MediaStore.Video.VideoColumns.DATE_ADDED
    };
Cursor cur = context.getContentResolver()
            .query(uri, projection, selection, selectionArgs, MediaStore.Images.ImageColumns.DATE_ADDED + " DESC");

Any idea how I can fix this query


Solution

  • The selection parameter in ContentResolver::query does only support WHERE clauses (without the WHERE keyword). docs

    Your approach is including the UNION clause in the selection which is invalid. If you need a union, you may have to do two separate queries and combine the two results yourself.

    EDIT

    For your specifc case the selection should be defined as follows

    selection = "bucket_id = ?"