How to create a Vcard of a selected contact from the Default Contacts application

I am working on an Android project where I need to generate a Vcard of a selected contact.

Although I can generate a vcard sample using this tool, But this gives us the ability to create a default vcard. He does not talk about how to retrieve a contact list, and then retrieve information about the selected contact, and then create his vcard.

Although I can open contact activity using the following code,

protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult(intent, 1); } @Override public void onActivityResult(int reqCode, int resultCode, Intent data) { super.onActivityResult(reqCode, resultCode, data); switch (reqCode) { case (1) : if (resultCode == Activity.RESULT_OK) { Uri contactData = data.getData(); // Cursor c = managedQuery(contactData, null, null, null, null); Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); String cname[] = new String[50]; cname = c.getColumnNames(); Log.d("Column Names", "--" + cname.toString()); if (c.moveToFirst()) { String name=c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // String name = c.getString(c.getColumnIndex(People.NAME)); for(int i=0; i<c.getColumnCount(); i++) { Log.d("Column Count", "--" + c.getColumnName(i)); } // String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); Log.d("Name", "-----" + name + phoneNumber); // TODO Whatever you want to do with the selected contact name. } } break; } } 

Now, I can get the number of the selected contact, now I need to attach the generateVcard class with this class to generate the vcard of the selected contact.

class gererateVcard -

 try { OutputStreamWriter writer = new OutputStreamWriter( new FileOutputStream("example.vcard"), "UTF-8"); VCardComposer composer = new VCardComposer(); //create a contact ContactStruct contact1 = new ContactStruct(); contact1.name = "Neo"; contact1.company = "The Company"; contact1.addPhone(Contacts.Phones.TYPE_MOBILE, "+123456789", null, true); //create vCard representation String vcardString = composer.createVCard(contact1, VCardComposer.VERSION_VCARD30_INT); Log.d("VCard String" , "----- " + vcardString); //write vCard to the output stream writer.write(vcardString); writer.write("\n"); //add empty lines between contacts // repeat for other contacts // ... writer.close(); } catch(Exception e) { Log.d("Error", "---- e -----" + e); } 
+4
source share

Source: https://habr.com/ru/post/1447408/


All Articles