Android 从通讯簿中选择联系人后,如何提取vCard并发送?我只想复制“发送到用户”功能

Android 从通讯簿中选择联系人后,如何提取vCard并发送?我只想复制“发送到用户”功能,android,Android,我使用此代码使用字符串格式在两个人之间交换联系。 但是我想通过message.plz帮助我交换vcard。或者发送一些详细的代码 这是我用来从保存的列表中选择联系人的代码 ActivityResultant请求代码、int-resultCode、意图数据上的受保护无效{ public void sendSms(View v) { EditText n1=(EditText)findViewById(R.id.num1); EditText n2=(EditText

我使用此代码使用字符串格式在两个人之间交换联系。 但是我想通过message.plz帮助我交换vcard。或者发送一些详细的代码

这是我用来从保存的列表中选择联系人的代码 ActivityResultant请求代码、int-resultCode、意图数据上的受保护无效{

 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);*/

 }

因此,第一步是获取vCard文件本身。 您可以使用光标抓取文件的路径:

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...


    }
}
现在,每个联系人的vcardPath中都存储了vCard的路径

要与他人分享,您只需用它表达一个意图:

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());
}
如果您想自己创建vcf文件,可以使用以下代码:

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);
请参阅此以查看格式选项

最新答复:

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

将vcardPath存储在某个地方,当您想要共享文件时,您可以使用我在上面写过的意图

我是否必须用单独的函数编写代码的第一部分??这取决于,它只是在手机上的所有联系人之间循环并获取vCard的路径。您是如何生成您的通讯簿的?实际上,首先我创建了一些点击并保存。之后,我将从该列表中选择联系人。因此,现在我想将所选联系人转换为.vcf文件。如何操作?请发布创建和保存联系人的代码。您可以手动创建vcf查看我的编辑,或者如果您能够在手机上存储您创建的联系人的id,您可以查询光标指向联系人并抓取vcf文件的路径。
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;   
        }
    }
}