1

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)
    ...
prom85
  • 16,896
  • 17
  • 122
  • 242

1 Answers1

2

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.

marmor
  • 27,641
  • 11
  • 107
  • 150
  • That's interesting. This means, if the user is restoring his contacts database with a root app like titanium backup, it may create an old database file for the contacts where this column is missing (BUT, his android system will think that it exists and will create a query that is not working with the underlying database file)... I'll check that – prom85 Jan 26 '17 at 11:07
  • yep, definitely, i've seen a ton of issues by users using titanium backup and similar, they can break easily. – marmor Jan 26 '17 at 11:09
  • thanks, that makes totally sense and I'm quite sure this is what happens... I try avoid it by explicitly defining the fields in my query I need, that will already help. But this explains the root cause. Thanks – prom85 Jan 26 '17 at 11:15
  • Hey, I got this error in the production environment, on non-rooted devices(as reported by Crashalitics) with operation systems 6,7,9 on a verity of devices. So I think the problem is something else – Ilya Gazman Jul 15 '19 at 00:34
  • @IlyaGazman are those devices of big known brands like Samsung? or are those mainly smaller brans / Chinese brands? – marmor Jul 15 '19 at 10:11
  • 8, users with 23 crashes on HTC(65%), OnePlus(13%), MIDCOM(9%), Blackview(4%), Other(9%) – Ilya Gazman Jul 15 '19 at 10:54