Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

Import Contacts from .vcf file in Android 2.1

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...