private fun exportToApp() { thread { var count = 0 for (contacti in contactList) { if (contacti.isChecked) { val contentResolver = getContentResolver() val contact = ContentValues().apply { put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contacti.name) } val uri: Uri? = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, contact) val rawContactId = uri?.lastPathSegment val phone = ContentValues().apply { put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId) put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE) put(ContactsContract.CommonDataKinds.Phone.NUMBER, contacti.phone) } contentResolver.insert(ContactsContract.Data.CONTENT_URI, phone) val email = ContentValues().apply { put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId) put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE) put(ContactsContract.CommonDataKinds.Email.ADDRESS, contacti.email) } contentResolver.insert(ContactsContract.Data.CONTENT_URI, email) count++ } } runOnUiThread { Toast.makeText(this,"已导出${count}位联系人!",Toast.LENGTH_LONG).show() } } finish()}这段代码出现了java.lang.IllegalArgumentException: raw_contact_id is required错误,请分析错误原因,并给出修改后的完整代码
时间: 2024-02-12 18:10:04 浏览: 74
这个错误是因为在插入电话和电子邮件时,没有使用正确的 rawContactId。可以通过将 rawContactId 赋值给变量,然后在插入电话和电子邮件时使用该变量来解决这个问题。以下是修改后的代码:
```kotlin
private fun exportToApp() {
thread {
var count = 0
for (contacti in contactList) {
if (contacti.isChecked) {
val contentResolver = getContentResolver()
val contact = ContentValues().apply {
put(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, contacti.name)
}
val uri: Uri? = contentResolver.insert(ContactsContract.RawContacts.CONTENT_URI, contact)
val rawContactId = uri?.lastPathSegment
// 插入电话
val phone = ContentValues().apply {
put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
put(ContactsContract.CommonDataKinds.Phone.NUMBER, contacti.phone)
}
contentResolver.insert(ContactsContract.Data.CONTENT_URI, phone)
// 插入电子邮件
val email = ContentValues().apply {
put(ContactsContract.Data.RAW_CONTACT_ID, rawContactId)
put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
put(ContactsContract.CommonDataKinds.Email.ADDRESS, contacti.email)
}
contentResolver.insert(ContactsContract.Data.CONTENT_URI, email)
count++
}
}
runOnUiThread {
Toast.makeText(this,"已导出${count}位联系人!",Toast.LENGTH_LONG).show()
}
finish()
}
}
```
在这个修改后的代码中,我们使用 `val rawContactId = uri?.lastPathSegment` 将获取到的 rawContactId 存储在变量 rawContactId 中,并在插入电话和电子邮件时使用该变量。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![css](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)