1

Android 如何发送多个联系人附加在单个 .vcf 文件中并发送到邮件?

我已经创建了所有联系人的 .vcf 文件,现在我想从 .vcf 文件中读取这些联系人,请帮助我,请不要提供此链接以读取http://code.google.com/p/vcardio/

我的 vcf 文件是这样创建的

BEGIN:VCARD
VERSION:2.1
N:;rohit;;;
FN:rohit
TEL;HOME;VOICE:855-904-6600
END:VCARD

BEGIN:VCARD
VERSION:2.1
N:;rks;;;
FN:rks
TEL;HOME;VOICE:887-276-5160
END:VCARD
BEGIN:VCARD
VERSION:2.1
N:kumar;rohit;;;
FN:rohit kumar
TEL;HOME;VOICE:981-442-3564
EMAIL;HOME:rks@gmail.com
END:VCARD
4

4 回答 4

2

SaveContact()您可以通过使用以下代码添加方法来解决此问题:

private void SaveContact()
{
    String DisplayName = "LUIS R. FERNANDEZ";
    String MobileNumber = "7863763161";
    String HomeNumber = null;
    String WorkNumber = "3056627325";
    String emailID = "LRFernandez@msn.com";
    String company = "Keller Williams";
    String jobTitle = null; //"BROKER ASSOCIATE";
    String website = "www.kellerwilliams.com";
    //String add_off = "4949 Ponce DeLeon Blvd Suite 400 Miami Florida FL 33146"; 
    String street = "4949 Ponce De Leon Blvd Suite 400";
    String city = "Miami";
    String state = "Florida FL";
    String zipcode = "33146";
    String country = "United States";
    Bitmap snap = BitmapFactory.decodeResource(getResources(), R.drawable.luis_snap);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    snap.compress(Bitmap.CompressFormat.JPEG , 100, stream);


    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    int rawContactInsertIndex = ops.size();

    ops.add(ContentProviderOperation
            .newInsert(ContactsContract.RawContacts.CONTENT_URI)
            .withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
            .withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
            .build());

    // exp
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                 .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                 .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
                 .withValue(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS, null)
                 .withValue(ContactsContract.CommonDataKinds.StructuredPostal.TYPE, ContactsContract.CommonDataKinds.StructuredPostal.TYPE_WORK)
                 .withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET, street)
                 .withValue(ContactsContract.CommonDataKinds.StructuredPostal.CITY, city)
                 .withValue(ContactsContract.CommonDataKinds.StructuredPostal.REGION, state)
                 .withValue(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE, zipcode)
                 .withValue(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY, country)
                 //.withValue(StructuredPostal.TYPE, StructuredPostal.TYPE_WORK)
                 .build());


    //INSERT imAGE
    ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
              .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
              .withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
              .withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, 
                    stream.toByteArray())
              .build());

    // ------------------------------------------------------ Names
    if (DisplayName != null) {
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
                .withValue(
                        ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME,
                        DisplayName).build());
    }
    // ------------------------------------------------------ Mobile Number
    if (MobileNumber != null) {
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                        MobileNumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE)
                .build());
    }

    // ------------------------------------------------------ Home Numbers
    if (HomeNumber != null) {
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                        HomeNumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_HOME)
                .build());
    }

    // ------------------------------------------------------ Work Numbers
    if (WorkNumber != null) {
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Phone.NUMBER,
                        WorkNumber)
                .withValue(ContactsContract.CommonDataKinds.Phone.TYPE,
                        ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
                .build());
    }

    // ------------------------------------------------------ Email
    if (emailID != null) {
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
                .withValue(ContactsContract.CommonDataKinds.Email.DATA,
                        emailID)
                .withValue(ContactsContract.CommonDataKinds.Email.TYPE,
                        ContactsContract.CommonDataKinds.Email.TYPE_WORK)
                .build());
    }

    // ------------------------------------------------------ Organization
    if (company != null || jobTitle != null) {
        ops.add(ContentProviderOperation
                .newInsert(ContactsContract.Data.CONTENT_URI)
                .withValueBackReference(
                        ContactsContract.Data.RAW_CONTACT_ID, 0)
                .withValue(
                        ContactsContract.Data.MIMETYPE,
                        ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.COMPANY,
                        company)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.TYPE,
                        ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.TITLE,
                        jobTitle)
                .withValue(
                        ContactsContract.CommonDataKinds.Organization.TYPE,
                        ContactsContract.CommonDataKinds.Organization.TYPE_WORK)
                .build());

        if (website != null)
        {
            ops.add(ContentProviderOperation
                    .newInsert(ContactsContract.Data.CONTENT_URI)
                         .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 
                                rawContactInsertIndex)
                         .withValue(ContactsContract.Data.MIMETYPE, 
                                ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE)
                         .withValue(ContactsContract.CommonDataKinds.Website.URL, 
                                website)
                         .withValue(ContactsContract.CommonDataKinds.Website.TYPE, 
                                ContactsContract.CommonDataKinds.Website.TYPE_WORK)
                         .build());
        }
    }

    // Asking the Contact provider to create a new contact
    try 
    {
        getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
        Toast.makeText(getApplicationContext(),"Contact saved successfully", Toast.LENGTH_SHORT).show();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
        //Toast.makeText(getApplicationContext(),"Exception: " + e.getMessage(), Toast.LENGTH_LONG).show();
    }
}
于 2013-02-08T06:53:09.757 回答
2

您可以使用ez-vcard对其进行解析:

try{
    File file = new File(Environment.getExternalStorageDirectory()+"/vcards.vcf");
    List<VCard> vcards = Ezvcard.parse(file).all();
    for (VCard vcard : vcards){
      System.out.println("Name: " + vcard.getFormattedName().getValue());
      System.out.println("Telephone numbers:");
      for (Telephone tel : vcard.getTelephoneNumbers()){
        System.out.println(tel.getTypes() + ": " + tel.getText());
      }
    }
}catch(Exception e){e.printStackTrace();}
于 2013-03-08T14:14:11.503 回答
1
VCardParser parser = new VCardParser();
            VDataBuilder builder = new VDataBuilder();

              //read whole file to string
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(file), "UTF-8"));

            String vcardString = "";
            String line;
            while ((line = reader.readLine())!= null) {
                vcardString += line + "\n";

            }
            reader.close();

            //parse the string
            boolean parsed = parser.parse(vcardString, "UTF-8", builder);
            if (!parsed) {
                throw new VCardException("Could not parse vCard file: " + file);
            }

            //get all parsed contacts
            List<VNode> pimContacts = builder.vNodeList;
            int c = pimContacts.size();
            pDialog.setMax(c);
            // do something for all the contacts

            for (VNode contact : pimContacts) {
                i++;
                String y = String.valueOf(i);
                ArrayList<PropertyNode> props = contact.propList;
                // contact name - FN property
                String HomeNumber = null;
                String DisplayName = null;
                String Add = null;
                String emailID = null;

                for (PropertyNode prop : props) {
                    if ("FN".equals(prop.propName)) {
                        DisplayName = prop.propValue;
                        // we have the name now
                        break;
                    }

                }
    }
于 2013-03-08T06:20:39.693 回答
0
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(storage_path+vfile)),"text/x-vcard"); //storage path is path of your vcf file and vFile is name of that file.
startActivity(intent);
于 2014-04-14T06:37:54.497 回答