I'm trying to Read Phone number of a Contact Selected using Contact Picker. The Display Name works fine, But Phone number doesn't. Code:
//calling Contact Picker
public void CPick(View v){
Intent intent=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
@Override
//Contact Picker here:
protected void onActivityResult(int reqCode, int resultCode, Intent data){
super.onActivityResult(reqCode,resultCode, data);
if (reqCode==PICK_CONTACT){
if(resultCode==AppCompatActivity.RESULT_OK){
Uri contatctData=data.getData();
Cursor c=getContentResolver().query(contatctData,null,null,null,null);
if (c.moveToFirst()){
//String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
//Above line works Fine
String name=c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
//Above line gives error on runtime "invalid column"
Toast.makeText(this,"U have picked:"+name,Toast.LENGTH_SHORT).show();
}
}
}
}
Anyhelp will be very Appreciated because I couldn't find relevant answer anywhere.
If you want to allow the user to pick a phone-number, the best option is to use a PHONE-PICKER
not a CONTACT-PICKER
:
Intent intent = new Intent(Intent.ACTION_PICK, CommonDataKinds.Phone.CONTENT_URI);
startActivityForResult(intent, PICK_PHONE);
...
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
if (requestCode == PICK_PHONE && resultCode == RESULT_OK){
Uri phoneUri = intent.getData();
Cursor cur = getContentResolver().query(phoneUri, new String[] { Phone.DISPLAY_NAME, Phone.NUMBER }, null, null, null);
if (cur != null && cur.moveToFirst()){
String name = cur.getString(0);
String number = cur.getString(1);
Log.d("PHONE-PICKER", "User picker: " + name + " - " + number);
cur.close();
}
}
}