Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android contacts extraction

Tags:

android

Are there any possibilities to extract contacts in some of the following formats, vCard, hCard or json/xml, using the standard Android API?


2 Answers

ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
AssetFileDescriptor fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();

Above code can be used to export the contacts in vcf format. Read all the contacts from the fileInputStream object "fis".

Also, don't forget to add the permissions in the manifest file -

<uses-permission android:name="android.permission.READ_CONTACTS" />

Can anyone help with importing the same .vcf file to the Android using some API?

like image 155
Prateek Jain Avatar answered Jun 29 '26 15:06

Prateek Jain


i believe CONTENT_VCARD_URI is what you're looking for, at least for android 2.0 (api level 5).

i just lucked up and found it.

like image 25
jason gilbert Avatar answered Jun 29 '26 14:06

jason gilbert