android从vcf文件中读取联系人

时间:2013-01-25 10:02:56

标签: android contacts vcf

Android How to send multiple contacts are attached in single .vcf file and send to mail?

我创建了所有联系人的.vcf文件,现在我想从.vcf文件中读取这些联系人,请帮助我和PLZ donot给这个链接阅读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 个答案:

答案 0 :(得分: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();
    }
}

答案 1 :(得分:1)

您可以使用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();}

答案 2 :(得分:0)

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;
                    }

                }
    }

答案 3 :(得分: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);