Unable to get Phone from registerForActivityResult(new ActivityResultContracts.PickContact()

123 Views Asked by At

I am trying to get Contact information. It used to work fine when for years, but since (today) I start using registerForActivityResult(new ActivityResultContracts.PickContact() I get various columns (listed below) for null projection, but I am unable to get phone and email directly. Also, it doesn't recognize Phone.CONTACT_ID

Can I get sample code to get both Phone and Email using registerForActivityResult.

last_time_contacted, phonetic_name, is_private, custom_ringtone, contact_status_ts, pinned, photo_id, photo_file_id,
contact_status_res_package, link_count, link, contact_chat_capability, contact_status_icon, display_name_alt, sort_key_alt, in_visible_group, starred, contact_status_label, 
phonebook_label, is_user_profile, has_phone_number, display_name_source, has_email, phonetic_name_style, send_to_voicemail, lookup, phonebook_label_alt, 
contact_last_updated_timestamp, sec_custom_vibration, photo_uri, phonebook_bucket, sec_preferred_sim, contact_status, display_name, sort_key, photo_thumb_uri, link_type1, 
contact_presence, sec_custom_alert, sec_led, display_name_reverse, in_default_directory, times_contacted, dirty_contact, _id, name_raw_contact_id, phonebook_bucket_alt

New Code (Very similar to old code)

Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);

cursor.moveToFirst();
       
id = cursor.getString(cursor.getColumnIndex(Phone.CONTACT_ID));
String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));

Old Code:

Intent pickContactIntent = new Intent(Intent.ACTION_PICK);
pickContactIntent.setType(Phone.CONTENT_TYPE);
startActivityForResult(pickContatIntent, PICK_CONTACT_REQUEST);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri contactUri = data.getData();
    String[] projection = {Phone.CONTACT_ID, Phone.NUMBER, Phone.DISPLAY_NAME};

    Cursor cursor = getContentResolver().query(contactUri, projection, null, null, null);
    cursor.moveToFirst();

    id = cursor.getString(cursor.getColumnIndex(Phone.CONTACT_ID));
    String number = cursor.getString(cursor.getColumnIndex(Phone.NUMBER));
    String name = cursor.getString(cursor.getColumnIndex(Phone.DISPLAY_NAME));
0

There are 0 best solutions below