8

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.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134

2 Answers2

10

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);
Community
  • 1
  • 1
Squonk
  • 48,735
  • 19
  • 103
  • 135
  • apparently, it happens if you click on a .vcf file in a file browser for example. – Jeffrey Blattman Jan 05 '11 at 18:39
  • @farble1670: I was going to respond as a comment but it was too long. See the point after EDIT: in my answer. – Squonk Jan 06 '11 at 00:53
  • thanks for your answer. that seems like it's worth a shot. would you have any idea what the intent would look like? i don't quite understand how i'd point the intent at a file. – Jeffrey Blattman Jan 06 '11 at 01:06
  • also, seems like there should be a way to set the data (text vcard) in the intent as data to avoid writing it to a file. i can't get that to work however. – Jeffrey Blattman Jan 06 '11 at 01:27
  • @farble1670: See EDIT2 if it's of any help. – Squonk Jan 06 '11 at 06:51
  • i actually arrived at the same solution playing around myself. yes, seems strange but i couldn't make it work without writing the vcard string to a temp file. i think people will find this useful, as googling around showed many folks with wondering how to make this happen. – Jeffrey Blattman Jan 06 '11 at 16:21
  • one more thing, a drawback of this approach ... ImportVCardActivity, the android activity that actually does the word, doesn't set a result code (i checked the source). that means that the starting activity has no way to determine if the import was successful. ImportVCardActivity does report success / error visually, but chances are it's not going to fit into your app's look and feel. – Jeffrey Blattman Jan 06 '11 at 16:30
  • @farble1670: "chances are it's not going to fit into your app's look and feel" - Well I did say it was a bit messy at the end of my first edit. It is the nature of Android development, however, to utilise either built-in or 3rd party activities and so on - as a result we do have to accept how they may look and feel. At least the user will recognise the visual responses as valid Android activity. If you feel there's a requirement out there, give it a try and put it out on the Market and see what user feedback you get. – Squonk Jan 06 '11 at 23:32
  • @Squonk "As far as I can tell there is a way to 'extract' a vCard relating to an individual contact". Can you elaborate? I saw the vCardIO app but I have yet to look at its code to see how they handle the extraction. I was thinking of retrieving a contact's data rows and place that info into a vCard format. – Dennis Dec 04 '12 at 09:18
0

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!!

Prateek Jain
  • 1,234
  • 14
  • 24