ContentProvider (contacts) - no such column: metadata_dirty

646 views Asked by At

I'm trying to get all contacts of a specific type from the phone like following:

Cursor cursor = context.getContentResolver().query(
            ContactsContract.RawContacts.CONTENT_URI,
            null,
            ContactsContract.RawContacts.ACCOUNT_TYPE + "='com.whatsapp'",
            null,
            ContactsContract.RawContacts.CONTACT_ID + " ASC");

But this line already throws an exception (custom rom, nougat => maybe it's related to this?). I only got this error from one user yet and I'm stuck here, does anyone know how to solve that? Is there an alternative way to query all contacts?

My exception looks like following:

Exception: android.database.sqlite.SQLiteException: no such column: metadata_dirty (code 1): , while compiling: 
SELECT sort_key, send_to_voicemail, pinned, display_name, metadata_dirty, 
phonebook_label_alt, version, phonebook_bucket, _id, custom_ringtone, 
times_contacted, account_type_and_data_set, sync4, dirty, sync2, 
contact_id, raw_contact_is_user_profile, aggregation_mode, data_set,
phonebook_label, account_type, sync3, display_name_alt, phonetic_name,
last_time_contacted, display_name_source, backup_id, sort_key_alt, 
phonebook_bucket_alt, deleted, starred, account_name, sync1, sourceid, 
phonetic_name_style 
FROM view_raw_contacts_restricted AS view_raw_contacts 
WHERE (1) 
AND ((account_type='com.whatsapp')) 
ORDER BY contact_id ASC
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java)
    at android.content.ContentProviderProxy.query(ContentProviderNative.java)
    at android.content.ContentResolver.query(ContentResolver.java)
    at android.content.ContentResolver.query(ContentResolver.java)
    ...
1

There are 1 answers

6
marmor On BEST ANSWER

I don't see anything wrong with your query, it seems like this is an internal bug in the user's custom ROM.

METADATA_DIRTY is a new column in Android N: https://developer.android.com/reference/android/provider/ContactsContract.RawContactsColumns.html#METADATA_DIRTY

So it seems like the system is Nougat, but the Contacts DB is based on an older version of Android.