Aggregate Contacts are added automatically?

北战南征 提交于 2019-12-01 12:06:52

问题


you must have seen this piece of code somewhere else too,but obviously there's no answer for this exception.

EDIT: IF You've come here finding a solution to restoring contacts via vcardio.This is IT!!

I got this while utilising the vCardIO api for android used to restore contacts from vcard to the contacts db.I have been using the following doImport() method which is supposed to work just fine,but it isnt!

 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:


Thanks,but no thanks.I managed it myself,Alhumdulillah.Make the following changes to

doImport();

Method of 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) {

    }
}


来源:https://stackoverflow.com/questions/9685149/aggregate-contacts-are-added-automatically

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