我有一个按钮打开并选择一个联系人的一些特定的数据-使用ActivityResultContracts.PickContact的名称和号码。使用本页的指南并进行一些修改,我能够成功检索联系人姓名。
我在清单文件中有这个权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
问题是我的应用程序崩溃每当我选择联系人获取它的号码。
抛出的异常是:
. lang。IllegalArgumentException: Invalid column data1
我是否可以了解更多正在发生的事情以及如何使其按预期工作?
///THE COMMENTED LINES ARE FOR CONTACT'S NAME AND IT WORKED PERFECTLY (COMMENT THE NUMBER LINES FOR TEST)
@Composable
@Preview
fun openAndSelectContact() {
val context = LocalContext.current
val launchContact = rememberLauncherForActivityResult(
contract = ActivityResultContracts.PickContact()
) {
val projection: Array<String> = arrayOf(
// ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER
)
context.contentResolver.query(
it!!,
projection,
null,
null,
null
)
.use { cursor ->
if (cursor!!.moveToFirst()) {
val numberIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
val number = cursor.getString(numberIndex)
Toast.makeText(context, "Number is $number!", Toast.LENGTH_SHORT).show()
// val nameIndex =
// cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
// val name = cursor.getString(nameIndex)
// Toast.makeText(context, "Name is $name!", Toast.LENGTH_SHORT)
// .show()
}
}
}
val launchContactPermission = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
launchContact.launch()
} else {
Toast.makeText(context, "Permission Denied!", Toast.LENGTH_SHORT)
.show()
}
}
Button(
content = { Text("IMPORT FROM CONTACT") },
onClick = {
when (PackageManager.PERMISSION_GRANTED) {
//First time asking for permission ... to be granted by user
ContextCompat.checkSelfPermission(
context,
Manifest.permission.READ_CONTACTS
) -> {
launchContact.launch()
}
else -> {
//If permission has been already granted
launchContactPermission.launch(Manifest.permission.READ_CONTACTS)
}
}
}
)
}
似乎使用ActivityResultContracts.PickContact()
我必须使用Intent修改代码和ActivityResultContracts.StartActivityForResult ()得到我想要的结果。下面是新代码@Composable
@Preview
fun openAndSelectContact() {
val context = LocalContext.current
//create a intent variable
val contactIntent = Intent(Intent.ACTION_PICK).apply {
type = ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE
}
val launchContactForResult = rememberLauncherForActivityResult(
contract = ActivityResultContracts.StartActivityForResult()
) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val contactUri: Uri? = result?.data?.data
val projection: Array<String> = arrayOf(
ContactsContract.CommonDataKinds.Phone.NUMBER,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
)
contactUri?.let {
context.contentResolver.query(it, projection, null, null, null).use { cursor ->
// If the cursor returned is valid, get the phone number and (or) name
if (cursor!!.moveToFirst()) {
val numberIndex =
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)
val number = cursor.getString(numberIndex)
val nameIndex =
cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)
val name = cursor.getString(nameIndex)
// Do something with the phone number
Toast.makeText(
context,
"Number is $number & Name is $name",
Toast.LENGTH_SHORT
).show()
}
}
}
}
}
val launchContactPermission = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
launchContactForResult.launch(contactIntent)
} else {
Toast.makeText(context, "Permission Denied!", Toast.LENGTH_SHORT)
.show()
}
}
Button(
content = { Text("IMPORT FROM CONTACT") },
onClick = {
when (PackageManager.PERMISSION_GRANTED) {
//First time asking for permission ... to be granted by user
ContextCompat.checkSelfPermission(
context,
Manifest.permission.READ_CONTACTS
) -> {
launchContactForResult.launch(contactIntent)
}
else -> {
//If permission has been already granted
launchContactPermission.launch(Manifest.permission.READ_CONTACTS)
}
}
}
)
}