本文介绍了聚合联系人会自动添加?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您一定见过这块code其他地方也是如此,但显然有此异常的答案。

编辑:如果你来这里寻找解决通过vcardio.This恢复接触的IT !!

同时利用 vCardIO API来我得到这个Android的用于从电子名片联系人恢复到了使用以下doImport()方法,这应该只是正常工作db.I有接触,但它不是!

 公共无效doImport(最后字符串文件名,最后布尔替换){
    尝试 {

        文件vcfFile =新的文件(文件名);

        最后的BufferedReader vcfBuffer =新的BufferedReader(新的FileReader(文件名),1048576);

        最终长MAXLEN = vcfFile.length();

        //开始冗长的操作在后台线程

        长importStatus = 0;

                SQLiteDatabase DB = mOpenHelper.getWritableDatabase();
                SQLiteStatement querySyncId = db.compileStatement(选择+ SYNCID +FROM+ SYNCDATA_TABLE_NAME +WHERE+ PERSONID +=?);
                SQLiteStatement queryPersonId = db.compileStatement(选择+ PERSONID +FROM+ SYNCDATA_TABLE_NAME +WHERE+ SYNCID +=?);
                SQLiteStatement insertSyncId = db.compileStatement(插入+ SYNCDATA_TABLE_NAME +(+ PERSONID +,+ SYNCID +)VALUES(,)?);
                db.close();
                联系parseContact =新联系(querySyncId,queryPersonId,insertSyncId);
                字符串波帕=;
                波帕= parseContact.getContent();
                尝试 {
                    长RET = 0;
                    做  {

                        RET = parseContact.parseVCard(vcfBuffer);
                        //这是代码段线有击败蓝军任何程序员的潜力,总是抛出一个异常!
                        parseContact.addContact(CO,0,真正的);
}而(RET大于0);

                    db.close();


                }赶上(例外五){

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

                }
 

解决方案

谢谢,但没有thanks.I管理它自己,Alhumdulillah.Make如下修改

doImport();

VCardIO方法:

 公共无效doImport(最后字符串文件名,最后布尔替换){
    尝试 {

        文件vcfFile =新的文件(文件名);

        最后的BufferedReader vcfBuffer =新的BufferedReader(新的FileReader(文件名),1048576);

        最终长MAXLEN = vcfFile.length();


        长importStatus = 0;

                SQLiteDatabase DB = mOpenHelper.getWritableDatabase();
                SQLiteStatement querySyncId = db.compileStatement(选择+ SYNCID +FROM+ SYNCDATA_TABLE_NAME +WHERE+ PERSONID +=?);
                SQLiteStatement queryPersonId = db.compileStatement(选择+ PERSONID +FROM+ SYNCDATA_TABLE_NAME +WHERE+ SYNCID +=?);
                SQLiteStatement insertSyncId = db.compileStatement(插入+ SYNCDATA_TABLE_NAME +(+ PERSONID +,+ SYNCID +)VALUES(,)?);
                db.close();
                联系parseContact =新联系(querySyncId,queryPersonId,insertSyncId);
                字符串波帕=;
                波帕= parseContact.getContent();
                尝试 {
                    长RET = 0;
                    做  {

                        RET = parseContact.parseVCard(vcfBuffer);

                        / * GOOGLE code是眼前这个开线,无法正常工作!
                        parseContact.addContact(CO,0,真正的); * /

                        如果(RET> = 0){
                            字符串显示名称= parseContact.displayName;


                            名单< RowData> MobileNumbers = parseContact.phones;
                            名单< RowData>地址= parseContact.addrs;
                            名单< RowData>即时= parseContact.ims;
                            名单< OrgData>组织之参与= parseContact.orgs;
                            字符串注= parseContact.notes;
                            byte []的D​​P = parseContact.photo;
                            字符串的生日= parseContact.birthday;


                            ContentResolver的CR = CO.getContentResolver();

                            名单< RowData>邮件= parseContact.emails;

                            尝试
                                {


                                //添加名称
                            ArrayList的< ContentProviderOperation> OPS =新的ArrayList< ContentProviderOperation>();
                            INT rawContactInsertIndex = ops.size();

                            ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                                    .withValue(RawContacts.ACCOUNT_TYPE,空)
                                    .withValue(RawContacts.ACCOUNT_NAME,空).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,显示名称)//姓名的人
                                    。建立());

                            // ADDING PHONES
                           对于(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
                           对于(RowData一:邮件)
                           {

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


                           }

                           //添加地址

                           对于(RowData地址:地址)
                           {
                              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


                           对于(OrgData组织:组织之参与)
                           {
                              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)

                                        。建立());
                           }

                           // ADDING即时消息

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

                           //添加注释

                           如果(注=空&安培;!&安培;!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,债券).build());

                           }

                           // ADDING照片

                           如果(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生日
                           如果(生日= 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,生日)
                                        .withValue(CommonDataKinds.Event.TYPE,CommonDataKinds.Event.TYPE_BIRTHDAY).build()); //人的数量

                           }


                                                    cr.applyBatch(ContactsContract.AUTHORITY,OPS);

                                                }
                                                赶上(例外五)
                                                {
                                                    e.printStackTrace();
                                                    Toast.makeText(CO,异常:+ e.toString()+Eebolra:+ e.getMessage(),Toast.LENGTH_SHORT).show();
                                                }


                            importStatus + = parseContact.getParseLen();


                        }
                        db.close();
                        }而(RET大于0);

                    db.close();

                }赶上(例外五){

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

                }



    }赶上(FileNotFoundException异常E){

    }
}
 

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

                }
解决方案

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

    }
}

这篇关于聚合联系人会自动添加?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 20:04