聚合联系人会自动添加吗?

时间:2012-03-13 13:41:04

标签: android contactscontract vcard

你必须在其他地方看到这段代码,但显然这个例外没有答案。

编辑:如果您来到这里寻找通过vcardio恢复联系人的解决方案。这就是IT !!

我使用vCardIO api for android用于恢复从vcard到联系人db的联系人。我一直在使用下面的doImport()方法,它应该可以正常工作,但它不是!

 public void doImport(final String fileName, final boolean replace) {
    try {

        File vcfFile = new File(fileName);

        final BufferedReader vcfBuffer = new BufferedReader(new FileReader(fileName),1048576);

        final long maxlen = vcfFile.length();

        // Start lengthy operation in a background thread

        long importStatus = 0;

                SQLiteDatabase db = mOpenHelper.getWritableDatabase();
                SQLiteStatement querySyncId = db.compileStatement("SELECT " + SYNCID + " FROM " + SYNCDATA_TABLE_NAME + " WHERE " + PERSONID + "=?");
                SQLiteStatement queryPersonId = db.compileStatement("SELECT " + PERSONID + " FROM " + SYNCDATA_TABLE_NAME + " WHERE " + SYNCID + "=?");
                SQLiteStatement insertSyncId = db.compileStatement("INSERT INTO  " + SYNCDATA_TABLE_NAME + " (" + PERSONID + "," + SYNCID + ") VALUES (?,?)");
                db.close();
                Contact parseContact = new Contact(querySyncId, queryPersonId, insertSyncId);
                String popa="";
                popa=parseContact.getContent();
                try {
                    long ret = 0;
                    do  {

                        ret = parseContact.parseVCard(vcfBuffer);
                        //this is the snippet line which has the potential to beat the blues out of any programmer,always throwing an exception!
                        parseContact.addContact(CO, 0, true);
} while (ret > 0);

                    db.close();


                } catch (Exception e) {

                     Toast.makeText(CO,"NO "+e.getMessage()+"-"+e.getLocalizedMessage()+"-"+e.toString(), Toast.LENGTH_SHORT).show();

                }

1 个答案:

答案 0 :(得分:0)

谢谢,但不,谢谢。我自己管理,Alhumdulillah。做了以下更改

doImport();

VCardIO的方法:

     public void doImport(final String fileName, final boolean replace) {
    try {

        File vcfFile = new File(fileName);

        final BufferedReader vcfBuffer = new BufferedReader(new FileReader(fileName),1048576);

        final long maxlen = vcfFile.length();


        long importStatus = 0;

                SQLiteDatabase db = mOpenHelper.getWritableDatabase();
                SQLiteStatement querySyncId = db.compileStatement("SELECT " + SYNCID + " FROM " + SYNCDATA_TABLE_NAME + " WHERE " + PERSONID + "=?");
                SQLiteStatement queryPersonId = db.compileStatement("SELECT " + PERSONID + " FROM " + SYNCDATA_TABLE_NAME + " WHERE " + SYNCID + "=?");
                SQLiteStatement insertSyncId = db.compileStatement("INSERT INTO  " + SYNCDATA_TABLE_NAME + " (" + PERSONID + "," + SYNCID + ") VALUES (?,?)");
                db.close();
                Contact parseContact = new Contact(querySyncId, queryPersonId, insertSyncId);
                String popa="";
                popa=parseContact.getContent();
                try {
                    long ret = 0;
                    do  {

                        ret = parseContact.parseVCard(vcfBuffer);

                        /* GOOGLE CODE IS JUST THIS ON LINE WHICH AIN'T WORKING!!
                        parseContact.addContact(CO, 0, true); */

                        if (ret >= 0) {
                            String DisplayName = parseContact.displayName;


                            List<RowData> MobileNumbers=parseContact.phones;
                            List <RowData> Addresses = parseContact.addrs;
                            List <RowData> IMs = parseContact.ims;
                            List <OrgData> Orgs = parseContact.orgs;
                            String Notes = parseContact.notes;
                            byte[] dp = parseContact.photo;
                            String BirthDay = parseContact.birthday;


                            ContentResolver cr = CO.getContentResolver();

                            List<RowData> mails=parseContact.emails;

                            try 
                                {


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

                            ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                                    .withValue(RawContacts.ACCOUNT_TYPE, null)
                                    .withValue(RawContacts.ACCOUNT_NAME, null).build());
                            ops.add(ContentProviderOperation
                                    .newInsert(ContactsContract.Data.CONTENT_URI)
                                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID,rawContactInsertIndex)
                                    .withValue(ContactsContract.Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
                                    .withValue(StructuredName.DISPLAY_NAME, DisplayName) // Name of the person
                                    .build());

                            //ADDING PHONES
                           for(RowData l : MobileNumbers)
                            {

                               {
                            ops.add(ContentProviderOperation
                                    .newInsert(ContactsContract.Data.CONTENT_URI)
                                    .withValueBackReference(
                                            ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)
                                    .withValue(ContactsContract.Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)

                                    .withValue(Phone.NUMBER,l.data) 
                                    .withValue(Phone.TYPE,l.type).build());
                               }

                            }
                           //ADDING MAILS
                           for(RowData a :mails)
                           {

                               {
                                   ops.add(ContentProviderOperation
                                        .newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(
                                                ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)

                                        .withValue(ContactsContract.Data.MIMETYPE, Email.CONTENT_ITEM_TYPE)

                                        .withValue(Email.DATA,a.data)
                                        .withValue(Email.TYPE, a.type).build()); 
                               }


                           }

                           //ADDING ADDRESSES

                           for(RowData add :Addresses)
                           {
                              ops.add(ContentProviderOperation
                                        .newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(
                                                ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)


                                        .withValue(ContactsContract.Data.MIMETYPE,StructuredPostal.CONTENT_ITEM_TYPE)

                                        .withValue(StructuredPostal.DATA,add.data) 
                                        .withValue(StructuredPostal.TYPE, add.type).build()); 
                           }

                           //ADDING ORGANISATIONS


                           for(OrgData org :Orgs)
                           {
                              ops.add(ContentProviderOperation
                                        .newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(
                                                ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)


                                        .withValue(ContactsContract.Data.MIMETYPE,Organization.CONTENT_ITEM_TYPE)

                                        .withValue(Organization.DATA,org.company) 
                                        .withValue(Organization.TYPE, org.type)
                                        .withValue(Organization.TITLE, org.title)
                                        .withValue(Organization.LABEL, org.customLabel)

                                        .build()); 
                           }

                           //ADDING IMs

                           for(RowData IM :IMs)
                           {
                              ops.add(ContentProviderOperation
                                        .newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(
                                                ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)

                                        .withValue(ContactsContract.Data.MIMETYPE,Im.CONTENT_ITEM_TYPE)

                                        .withValue(Im.DATA,IM.data) 
                                        .withValue(Im.TYPE, IM.type).build()); 
                           }

                           //ADDING NOTES

                           if(Notes!=null && !Notes.equals(""))
                           {
                              ops.add(ContentProviderOperation
                                        .newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(
                                                ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)


                                        .withValue(ContactsContract.Data.MIMETYPE,Note.CONTENT_ITEM_TYPE)

                                        .withValue(Note.NOTE,Notes).build()); 

                           }

                           // ADDING PHOTO

                           if(dp!=null)
                           {
                              ops.add(ContentProviderOperation
                                        .newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(
                                                ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)


                                        .withValue(ContactsContract.Data.MIMETYPE,Photo.CONTENT_ITEM_TYPE)

                                        .withValue(Photo.PHOTO,dp).build());

                           }

                           //ADDING BIRTHDAY
                           if(BirthDay!=null && !BirthDay.equals(""))
                           {
                              ops.add(ContentProviderOperation
                                        .newInsert(ContactsContract.Data.CONTENT_URI)
                                        .withValueBackReference(
                                                ContactsContract.Data.RAW_CONTACT_ID,   rawContactInsertIndex)


                                        .withValue(ContactsContract.Data.MIMETYPE,CommonDataKinds.Event.CONTENT_ITEM_TYPE)

                                        .withValue(CommonDataKinds.Event.START_DATE,BirthDay)
                                        .withValue(CommonDataKinds.Event.TYPE,CommonDataKinds.Event.TYPE_BIRTHDAY).build()); // Number of the person

                           }


                                                    cr.applyBatch(ContactsContract.AUTHORITY, ops);

                                                } 
                                                catch (Exception e) 
                                                {               
                                                    e.printStackTrace();
                                                    Toast.makeText(CO, "Exception: "+e.toString()+"Eebolra:" + e.getMessage(), Toast.LENGTH_SHORT).show();
                                                }


                            importStatus += parseContact.getParseLen();


                        }
                        db.close();
                        } while (ret > 0);

                    db.close();

                } catch (Exception e) {

                     Toast.makeText(CO,"NO "+e.getMessage()+"-"+e.getLocalizedMessage()+"-"+e.toString(), Toast.LENGTH_SHORT).show();

                }



    } catch (FileNotFoundException e) {

    }
}