Get MIME contacts in Android

I want to get a contact list based on their MIME type in Android.

For example, I need a list of contacts that have email addresses.

+4
source share
2 answers

You must read the raw contact along with all the data associated with it using the ContactsContract.RawContacts.Entity directory. If the source contact has data rows, the entity cursor will contain a row for each data row. If the raw contact does not have data rows, the cursor will still contain one row with information about the level of the original contact.

 Uri rawContactUri = ContentUris.withAppendedId(RawContacts.CONTENT_URI, rawContactId); Uri entityUri = Uri.withAppendedPath(rawContactUri, Entity.CONTENT_DIRECTORY); Cursor c = getContentResolver().query( entityUri, new String[] { RawContacts.SOURCE_ID, Entity.DATA_ID, Entity.MIMETYPE, Entity.DATA1 }, null, null, null); try { while (c.moveToNext()) { String sourceId = c.getString(0); if (!c.isNull(1)) { String mimeType = c.getString(2); String data = c.getString(3); //decide here based on mimeType, see comment later } } } finally { c.close(); } 

For example, if mimeType is Phone.CONTENT_ITEM_TYPE , then column DATA1 stores the phone number, but if the data type is Email.CONTENT_ITEM_TYPE , then DATA1 stores the email address.

+2
source

To be universal and promising, you can use, for example, CommonDataKinds.Email , which provide constants for content types and column names of the corresponding data so that you can write something like

 String email = c.getString(c.getColumnIndex(Email.ADDRESS)); 

And you can, of course, provide a ConditionResolver.query ContentResolver.query() condition to retrieve only the records of interest, using MIMETYPE and other constants from the DataColumns .

+1
source

Source: https://habr.com/ru/post/1392050/


All Articles