1

Android 複数の連絡先を 1 つの .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 に答える
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 に答える