Question

i'm wondering if there's a clean way to import a vcard as an android contact. i have a vcard parser, but mapping each possible vcard field and field type is going to be a painful, bug-prone exercise. is there a better way?

an android contact looks suspiciously like a vcard, so i suspect they use vcards internally. there's no public API for this however.

Was it helpful?

Solution

an android contact looks suspiciously like a vcard, so i suspect they use vcards internally.

I think it's a lot more complicated than using 'vCards internally' especially as vCard is effectively just a text file format making it inefficient to store and more so to search a large number of them.

The Overview of ContactsContract (note I cut it at the point it mentions 'three-tier data model')...

Overview

ContactsContract defines an extensible database of contact-related information. Contact information is stored in a three-tier data model:

As far as I can tell there is a way to 'extract' a vCard relating to an individual contact but I don't know of one to create a contact from a vCard.

EDIT: WRT importing vCards when clicking a link in a browser - yes, good point. I can confirm that it works when receiving a vCard by Bluetooth at least (which is, of course, quite logical). If I 'open' the vCard on my phone, I get a 'chooser' asking me if I want to use 'File Editor' or 'People' (the HTC contacts app - not sure if it's called the same on other phones). If there is only one vCard then it imports without further prompting. If there are more than one, I get another chooser asking if I want to import One/Multiple/All (Multiple gives another chooser with checkboxes to make my selection).

In theory I suppose, it might be possible to have a whole load of .vcf files dumped in a directory somewhere and write some code which simply creates an Intent to 'open' one of them and use that with startActivity() without specifying which activity to use. I'm not sure which intent action would be being used here but I would start with ACTION_VIEW - other choices might be ACTION_EDIT or (tenuously) ACTION_RUN.

Although this may work in a known environment particularly as a one off, it is a bit messy and behaviour/results may vary with different phones, versions of Android and contacts apps.

EDIT2: WRT to using an Intent to process .vcf files, I've only tried it from my SD card - I have no idea how to do it without saving it first. The following reproduces exactly what I see when I open a vCard that's sent via Bluetooth...

Intent i = new Intent();
i.setAction(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.parse("file:///mnt/sdcard/downloads/bluetooth/MickeyMouse.vcf"), "text/x-vcard");
startActivity(i);

OTHER TIPS

I struggled a lot for importing contacts as a vCard in Android and after spending a lot of time I came to there are no public API's for importing contacts.

Only way in my knowledge is to reuse the source code of the Android. I achieved the same by reusing the Android 2.1 source code.

The files can be referred from

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/pim/vcard/exception/VCardException.java/

At right hand side explorer you will find all the files needed under the pim folder. The file which will be responsible to start the procedure is ImportVCardActivity.java

I hope it helps!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top