user3363266
user3363266

Reputation: 1

Once a contact is selected from address book, how do I extract vCard and send it? All I want to do is to replicate 'Send to User' functionality

 public void sendSms(View v)
 {

     EditText n1=(EditText)findViewById(R.id.num1);
        EditText n2=(EditText)findViewById(R.id.num2);
        //EditText msg=(EditText)findViewById(R.id.sms);

        //Button bt=(Button)findViewById(R.id.send);
     String phone_Num1= n1.getText().toString();
     String phone_Num2= n2.getText().toString();
    // String send_msg=msg.getText().toString();
    // Toast.makeText(getApplicationContext(), phone_Num1, Toast.LENGTH_LONG).show();
     //SmsManager sms=SmsManager.getDefault();
      getVcardString();
    /* PendingIntent piSent=PendingIntent.getBroadcast(this, 0, new Intent("SMS_SENT"), 0);
     PendingIntent piDelivered=PendingIntent.getBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0);
     sms.sendTextMessage( phone_Num1,null,"Contact this number "+phone_Num2.toString(),  piSent, piDelivered);
     sms.sendTextMessage( phone_Num2,null,"Contact this number "+phone_Num1.toString(),  piSent, piDelivered);*/

 }

I used this code to exchange contact between two people using string format. But i want to exchange vcard through message.plz help me.or send some code in detail

this is the code which i'm using to select contact from saved list protected void onActivityResult(int requestCode, int resultCode, Intent data) {

int n=0;
int n1=0;
// Check which request it is that we're responding to
if (requestCode == PICK_CONTACT_REQUEST) {

    // Make sure the request was successful
    if (resultCode == RESULT_OK) {
        // Get the URI that points to the selected conta
        Uri contactUri = data.getData();
        // We only need the NUMBER column, because there will be only one row in the result
        String[] projection = {Phone.NUMBER};

        // Perform the query on the contact to get the NUMBER column
        // We don't need a selection or sort order (there's only one result for the given URI)
        // CAUTION: The query() method should be called from a separate thread to avoid blocking
        // your app's UI thread. (For simplicity of the sample, this code doesn't do that.)
        // Consider using CursorLoader to perform the query.
        Cursor cursor = getContentResolver()
                .query(contactUri, projection, null, null, null);
        cursor.moveToFirst();

        // Retrieve the phone number from the NUMBER column
        int column = cursor.getColumnIndex(Phone.NUMBER);
        String number = cursor.getString(column);
        n++;
        number = number.replace("-" ,"");
       // Toast.makeText(getApplicationContext(), number, Toast.LENGTH_SHORT).show();


        EditText no = (EditText) findViewById(R.id.num1);
        if(!no.equals(" "))
        {
            String no_t = number+no.getText();
            no.setText(no_t);
        }

         //Toast.makeText(getApplicationContext(), n, Toast.LENGTH_SHORT).show();



        for(int i=0;i<num.length;i++)
        {

       num[i]=number;   
       // Toast.makeText(getApplicationContext(), num[i], Toast.LENGTH_SHORT).show();

        }

        // Do something with the phone number...


    }
}

Upvotes: 0

Views: 527

Answers (1)

Nyx
Nyx

Reputation: 2243

So the first step is to get the vCard file itself. You can do that by using a cursor to grab the path to the file:

ContentResolver contentResolver = context.getContentResolver();
String[] projection = new String[]{ContactsContract.Contacts.LOOKUP_KEY};
Cursor contacts =  contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, projection, null,  null, null);
if(contacts.moveToFirst()){
    do{
           String path = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
           Uri vcardPath = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, path);
      } while (contacts.moveToNext());
}

So now you have the path to vCard stored in vcardPath for each contact.

To share it, you just need to fire off an intent with it:

Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/vcard");
intent.putExtra(Intent.EXTRA_STREAM, vcardPath);
ShareActionProvider provider = new ShareActionProvider(context);
provider.setShareIntent(intent);

If you want to create the vcf file yourself, you can use this code:

File vcfFile = new File(this.getExternalFilesDir(null), "generated.vcf");
FileWriter fw = new FileWriter(vcfFile);
fw.write("BEGIN:VCARD\r\n");
fw.write("VERSION:3.0\r\n");
fw.write("N:" + lastName + ";" + firstName + "\r\n");
fw.write("FN:" + firstName + " " + lastName + "\r\n");
fw.write("ORG:" + companyName + "\r\n");
fw.write("TITLE:" + title + "\r\n");
fw.write("TEL;TYPE=WORK,VOICE:" + workPhone + "\r\n");
fw.write("TEL;TYPE=HOME,VOICE:" + homePhone + "\r\n");
fw.write("ADR;TYPE=WORK:;;" + street + ";" + city + ";" + state + ";" + postalCode + ";" + country + "\r\n");
fw.write("EMAIL;TYPE=PREF,INTERNET:" + emailAddress + "\r\n");
fw.write("END:VCARD\r\n");
fw.close();

Refer to this http://en.wikipedia.org/wiki/VCard to see format options


Updated answer:

if (requestCode == PICK_CONTACT_REQUEST) {
    if (resultCode == RESULT_OK) {
        Uri contactUri = data.getData();
        // We only need the NUMBER column, because there will be only one row in the result
        String[] projection = {Phone.NUMBER, ContactsContract.Contacts.LOOKUP_KEY};
        Cursor cursor = getContentResolver()
            .query(contactUri, projection, null, null, null);
        cursor.moveToFirst();
        int column = cursor.getColumnIndex(Phone.NUMBER);

        //Grab the vCard path
        int lookupKey = cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY);
        Uri vcardPath = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI,     cursor.getString(lookupKey));

        String number = cursor.getString(column);
        n++;
        number = number.replace("-" ,"");
        EditText no = (EditText) findViewById(R.id.num1);
        if(!no.equals(" "))
        {
            String no_t = number+no.getText();
            no.setText(no_t);
        }
        for(int i=0;i<num.length;i++)
        {
           num[i]=number;   
        }
    }
}

Store the vcardPath somewhere and when you want to share the file you use the intent that I have written above

Upvotes: 0

Related Questions