Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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_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);
                        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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
186 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...