【问题标题】:Store contact in phone contact list in Android from a file从文件中将联系人存储在 Android 中的电话联系人列表中
【发布时间】:2012-10-30 09:41:25
【问题描述】:

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

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

它正在运行,没有任何错误。但是当从手机的联系人列表中查看添加的联系人时,会出现错误消息"Unfortunately contacts have stopped working"

代码有什么问题?

【问题讨论】:

  • 这里的type属性值为“1”、“2”、...
  • 发布崩溃的堆栈跟踪

标签: android android-contacts


【解决方案1】:

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

但是在尝试之前先看看如果你将"(Phone.TYPE ,type)" 更改为"(Phone.TYPE, Phone.TYPE_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);
    }
}

【讨论】:

    【解决方案2】:

    没有为这个问题附加日志所以它是一个尝试。

    您的联系人提供商 (db) 可能支持“RawContacts.ACCOUNT_TYPE”、“RawContacts.ACCOUNT_NAME”列为 NULL,但在执行 Onclick 操作时,联系人详细信息视图需要该类型的一些数据或名称不应该为空,所以它崩溃了

    因此打开 raw_contact 表并找到其他联系人的“ACCOUNT_TYPE”和“ACCOUNT_NAME”列的正确默认值。并在您的构建器操作中使用相同的值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-12-01
      • 2014-03-30
      • 2015-12-16
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多