android read contacts from vcf file

假装没事ソ 提交于 2019-12-08 06:20:28
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;
                    }

                }
    }

You can solve this issue by adding a method SaveContact() with the following code:

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

You can use ez-vcard to parse it:

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();}
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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!