I am able to retrieve all contacts from android in .vcf file using following code.
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_U开发者_开发技巧RI,null, null, null, null);
String lookupKey = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
System.out.println("The value is " + cr.getType(uri));
AssetFileDescriptor fd = this.getContentResolver().openAssetFileDescriptor(uri, "r");
FileInputStream fis = fd.createInputStream();
I don't know how to use this .vcf file to import all these contacts using code. The .vcf file contains all the details of all contacts including photos etc.
Cheers, Prateek
As stated above, there is a built in VCFImportActivity baked in to the code of android itself. I personally wanted to open an unrecognized vcf file from the gmail app via intents. I would send an intent with the vcf data attached to my program which would then launch contacts app. If you save the above vcf file on to the root directory of the sd card ( or wherever the contacts app saves its exported vcfs), and then start an activity like so:
Uri stuff = getIntent().getData();
Intent i = new Intent(android.content.Intent.ACTION_VIEW, stuff);
i.setType("text/x-vcard");
startActivity(i);
Should start the contacts app on importing any vcf it sees at that directory. So obviously, save that vcf file before you launch this code snippet, launch the contacts app (via a chooser that will come up maybe), et voila! Android SHOULD start importing those contacts. [This is a solution off the top of my head]
If this doesn't, let me know and let me see what debug errors you get.
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 !!
Cheers, Prateek
I finally achieved the contacts import. The solution is to extract all the related files from Android source code and make few changes and it will work. :-)
精彩评论