Select 并使用 Intent 和 ActivityResultContracts 提取联系方式

Select and extract contact details using Intent and ActivityResultContracts

我有一个打开按钮和 select 一些特定数据的联系人 - 使用 ActivityResultContracts.PickContact. Using the guide from this page 和一些修改的姓名和号码,我能够成功检索联系人姓名。

我的清单文件中有这个以获得许可

<uses-permission android:name="android.permission.READ_CONTACTS" />

问题是每当我 select 联系人获取号码时我的应用程序崩溃。

抛出的异常是:

java.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() 是问题所在。

我不得不使用 IntentActivityResultContracts.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)
                }
            }
        }
    )
}