将联系人从文件存储到Android手机联系人列表中

将联系人从文件存储到Android手机联系人列表中,android,android-contacts,Android,Android Contacts,我正在使用此代码将文件中的联系人存储到手机的联系人列表中: public void addContacts(String name, String number, String type) { int backRefIndex = 0; String data=name+"--"+number+"--"+type; ArrayList<ContentProviderOperation> ops = new ArrayList<Content

我正在使用此代码将文件中的联系人存储到手机的联系人列表中:

public void addContacts(String name, String number, String type) {
    int backRefIndex = 0;       
    String data=name+"--"+number+"--"+type;
    ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
    Toast.makeText(getApplicationContext(), data, Toast.LENGTH_LONG).show();

    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, backRefIndex)
         .withValue(ContactsContract.Data.MIMETYPE
              ,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
         .withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
         .build());
    ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
         .withValueBackReference(Data.RAW_CONTACT_ID, backRefIndex)
         .withValue(Phone.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
         .withValue(Phone.NUMBER, number)
         .withValue(Phone.TYPE, type).build());
    try {
         getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) { }
}    
public void addContacts(字符串名称、字符串编号、字符串类型){
int backRefIndex=0;
字符串数据=名称+“--”+数字+“--”+类型;
ArrayList ops=新的ArrayList();
Toast.makeText(getApplicationContext(),数据,Toast.LENGTH_LONG.show();
添加(ContentProviderOperation.newInsert(RawContacts.CONTENT\uURI)
.withValue(RawContacts.ACCOUNT_类型,null)
.withValue(RawContacts.ACCOUNT\u名称,空)
.build());
添加(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT\uURI)
.带ValueBackReference(contacts contract.Data.RAW\u CONTACT\u ID,backRefIndex)
.withValue(contacts contract.Data.MIMETYPE
,ContactsContract.CommonDataTypes.StructuredName.CONTENT\u ITEM\u TYPE)
.withValue(contacts contract.CommonDataTypes.StructuredName.DISPLAY\u NAME,NAME)
.build());
add(ContentProviderOperation.newInsert(Data.CONTENT\u URI)
.带ValueBackReference(Data.RAW\u CONTACT\u ID,backRefIndex)
.withValue(Phone.MIMETYPE、Phone.CONTENT\u ITEM\u TYPE)
.withValue(电话号码、号码)
.withValue(Phone.TYPE,TYPE).build());
试一试{
getContentResolver().applyBatch(contacts contract.AUTHORITY,ops);
}捕获(例外e){}
}    
它正在运行,没有任何错误。但是,当从手机的联系人列表中查看添加的联系人时,会显示一条错误消息
“不幸的是,联系人已停止工作”


代码中有什么错误?

我也有同样的错误。您可以在我的方法中的注释代码中看到导致它的原因。你的错误必须是相似的。您的错误也可能是由于某些信息丢失造成的

但在尝试之前,先看看如果将
(Phone.TYPE,TYPE)
更改为
(Phone.TYPE,Phone.TYPE\u MOBILE)
,会发生什么。我认为这是你的错误,就像Sumit Chawla在评论中说的那样

添加空值检查是一种很好的做法。他们可以抛出异常

public void addContacts(String name, String number, int numberType
    , String email, String organization, String street, String city
    , String region, String postcode) {
    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(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID,rawContactInsertIndex)
            .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE)
            .withValue(StructuredName.DISPLAY_NAME, name)
            .build());

    ops.add(ContentProviderOperation
            .newInsert(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID,   rawContactInsertIndex)
            .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE)
            .withValue(Phone.NUMBER, number)
            .withValue(Phone.TYPE, numberType)
          //.withValue(Phone.TYPE, Phone.TYPE_MOBILE) //Use constants for type
            .build());

    ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
            .withValueBackReference(Data.RAW_CONTACT_ID, 0)
            .withValue(Data.MIMETYPE,Email.CONTENT_ITEM_TYPE)
            .withValue(Email.DATA, email)
            //If I add Email.TYPE People(Phone's contacts application) 
            //doesn't work any more.
            //The error is: "Unfortunately contacts have stopped working"
            // .withValue(Email.TYPE,Email.TYPE_MOBILE)
            .build());

    ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
           .withValueBackReference(Data.RAW_CONTACT_ID, 0)
           .withValue(Data.MIMETYPE,Organization.CONTENT_ITEM_TYPE)
           .withValue(Organization.COMPANY, organization)
           .build());

    ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
           .withValueBackReference(Data.RAW_CONTACT_ID, 0)
           .withValue(Data.MIMETYPE,StructuredPostal.CONTENT_ITEM_TYPE)
           .withValue(StructuredPostal.STREET, street)
           .withValue(StructuredPostal.CITY, city)
           .withValue(StructuredPostal.REGION, region)
           .withValue(StructuredPostal.POSTCODE, postcode)
           .build());

    try {
        mContext.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
    } catch (Exception e) { 
        Log.e(TAG, "Error adding contact!", e);
    }
}
public void addContacts(字符串名称、字符串编号、整数类型
、字符串电子邮件、字符串组织、字符串街道、字符串城市
,字符串区域,字符串邮政编码){
ArrayList ops=新的ArrayList();
int rawContactInsertIndex=ops.size();
添加(ContentProviderOperation.newInsert(RawContacts.CONTENT\uURI)
.withValue(RawContacts.ACCOUNT_类型,null)
.withValue(RawContacts.ACCOUNT\u名称,空)
.build());
添加操作(ContentProviderOperation
.newInsert(Data.CONTENT\u URI)
.带ValueBackReference(数据.原始联系人ID,原始联系人插入索引)
.withValue(Data.MIMETYPE、StructuredName.CONTENT\u ITEM\u TYPE)
.withValue(StructuredName.DISPLAY\u NAME,NAME)
.build());
添加操作(ContentProviderOperation
.newInsert(Data.CONTENT\u URI)
.带ValueBackReference(Data.RAW\u CONTACT\u ID,RAW contactInsertIndex)
.withValue(Data.MIMETYPE、Phone.CONTENT\u ITEM\u TYPE)
.withValue(电话号码、号码)
.withValue(Phone.TYPE、numberType)
//.withValue(Phone.TYPE,Phone.TYPE\u MOBILE)//对类型使用常量
.build());
add(ContentProviderOperation.newInsert(Data.CONTENT\u URI)
.带ValueBackReference(Data.RAW\u CONTACT\u ID,0)
.withValue(Data.MIMETYPE、Email.CONTENT\u ITEM\u TYPE)
.withValue(Email.DATA,Email)
//如果我添加Email.TYPE联系人(手机的联系人应用程序)
//再也不行了。
//错误是:“不幸的是,联系人已停止工作”
//.withValue(Email.TYPE,Email.TYPE_MOBILE)
.build());
add(ContentProviderOperation.newInsert(Data.CONTENT\u URI)
.带ValueBackReference(Data.RAW\u CONTACT\u ID,0)
.withValue(数据.模拟类型,组织.内容\项目\类型)
.withValue(组织、公司、组织)
.build());
add(ContentProviderOperation.newInsert(Data.CONTENT\u URI)
.带ValueBackReference(Data.RAW\u CONTACT\u ID,0)
.withValue(Data.MIMETYPE、StructuredPostal.CONTENT\u ITEM\u TYPE)
.withValue(StructuredPostal.STREET,STREET)
.withValue(StructuredPostal.CITY,CITY)
.withValue(StructuredPostal.REGION,REGION)
.withValue(StructuredPostal.POSTCODE,POSTCODE)
.build());
试一试{
mContext.getContentResolver().applyBatch(contacts contract.AUTHORITY,ops);
}捕获(例外e){
Log.e(标记“添加联系人时出错!”,e);
}
}

此问题没有附加日志,因此这是一次尝试

您的联系人提供程序(db)可能支持“RawContacts.ACCOUNT\u TYPE”、“RawContacts.ACCOUNT\u NAME”列为NULL,但在执行Onclick操作时,联系人详细信息视图需要该类型或名称的一些数据,因此其崩溃


所以打开原始联系人表,找到其他联系人的“帐户类型”和“帐户名称”列的正确默认值。并在生成器操作中使用相同的值。

此处的type属性的值为“1”、“2”…张贴崩溃的堆栈跟踪