I am using ContactsContract api intent for showing all contacts from an activity. This intent returns an id of the contact. I need to get the postal address of this contact.
Here is the code which i am using for showing contacts:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
startActivityForResult(intent, PICK_CONTACT);
I am getting the result in the onActivityResult function.
Please help, how do i do this.
in your onActivityResult, first get the contact ID and query the ContactsContract.Data for the relevant data:
// get the contact ID
Cursor cursor = getContentResolver().query(data.getData(), null, null, null, null);
cursor.moveToFirst();
long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.Contacts._ID));
cursor.close();
// get the data package containg the postal information for the contact
cursor = getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{ StructuredPostal.STREET,
StructuredPostal.CITY,
// add more coluns from StructuredPostal if you need them
StructuredPostal.POSTCODE},
ContactsContract.Data.CONTACT_ID + "=? AND " +
StructuredPostal.MIMETYPE + "=?",
new String[]{String.valueOf(id), StructuredPostal.CONTENT_ITEM_TYPE},
null);
Street = cursor.getString(cursor.getColumnIndex(StructuredPostal.STREET));
Postcode = cursor.getString(cursor.getColumnIndex(StructuredPostal.POSTCODE));
City = cursor.getString(cursor.getColumnIndex(StructuredPostal.CITY)));
// etc.
Can be done this way too (little lesser code)
Send the Intent this way:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_TYPE); startActivityForResult(intent, PICK_CONTACT);
In the onActivityResult method
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String address = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
Kiran's answer probably works fine for Android 2.0 and above, but when you want it to be compatible for Android 1.6 and below you need to adapt the code a bit:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(Contacts.ContactMethods.CONTENT_POSTAL_TYPE);
startActivityForResult(intent, PICK_CONTACT);
And in onActivtyResult method:
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String address = c.getString(c.getColumnIndexOrThrow(Contacts.ContactMethodsColumns.DATA));
}
Where PICK_CONTACT is a int of your own choice. (Right?)
This will return all the addresses from the contact Postal Address.
Uri uri = ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI;
String sortOrder = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor cr = getContentResolver().query(uri,null, null, null, sortOrder);
while(cr.moveToNext())
{
String Street = cr.getString(cr.getColumnIndex(StructuredPostal.STREET));
String Postcode = cr.getString(cr.getColumnIndex(StructuredPostal.POSTCODE));
String City = cr.getString(cr.getColumnIndex(StructuredPostal.CITY));
Toast.makeText(this,"Address is : " +City+","+Street ,Toast.LENGTH_SHORT).show();
}
cr.close();
Related
I.m new to Android Contact's, I wanted to return a contact id/ contact from below URi:
content://com.android.contacts/contacts/lookup/encoded?directory=2147483647#{\"display_name\":\"\u202A9500249707\u202C\",\"display_name_source\":20,\"vnd.android.cursor.item\\/contact\":{\"vnd.android.cursor.item\\/phone_v2\":[{\"data1\":\"123456789\"}
]}}
which is start with encoded?directory=2147483647..
can any one suggestion me here!!
In order to fetch the contact from the external storage of the phone, we have to understand about content Resolver,Cursor which you can learn on the android documentation.As you have asked in question, to handle the contact and it's related data, we can use following code.
Uri contactUri = data.getData();
Cursor cursor = null;
try {
String phoneNo = null;
cursor = getContentResolver()
.query(contactUri, null, null, null, null);
if (cursor.getCount() == 0) return;
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
int phoneIndex = cursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER);
phoneNo = cursor.getString(phoneIndex);
} finally {
cursor.close();
}
From this sample code,We can extract the necessary data using content Resolver and content provider.
But first, we have to pass content uri through intent as below to use the above code.
pickContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
I'm trying to access the contact name, phone number, and address of a person which I selected from contact picker intent.
Here is the code which I am using to open the contact picker Intent :
Here is the code which I am using :
Intent intent = new Intent(Intent.ACTION_PICK,
ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
case PICK_CONTACT:
if (resultCode == Activity.RESULT_OK) {
System.out.println("in on ActivityResult");
Uri contactData = data.getData();
Cursor c = getActivity().managedQuery(contactData, null,
null, null, null);
if (c.moveToFirst()) {
String id =
c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));StringhasPhone=c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones getActivity().getContentResolver().query(ContactsContract.CommonDataKind s.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
phones.moveToFirst();
String cNumber =
phones.getString(phones.getColumnIndex("data1"));String name phones.getString(phones.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
//here you can find out all the thing.
System.out.println("NAME:"+name);
}Cursor postal_cursor=getActivity().getContentResolver().query(ContactsContract.Data.CONTENT_URI,
new String[]{
ContactsContract.CommonDataKinds.StructuredPostal.STREET,
ContactsContract.CommonDataKinds.StructuredPostal.CITY,
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS},
ContactsContract.Data.CONTACT_ID + "=? AND " +
ContactsContract.CommonDataKinds.StructuredPostal.MIMETYPE + "=?",
new String[]{String.valueOf(id),
ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE},
null);
postal_cursor.moveToFirst();
while(postal_cursor.moveToNext())
{
String Strt =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.STREET));
String Cty =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.CITY));
String cntry =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
String address =
postal_cursor.getString(postal_cursor.getColumnIndex(
ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
}
postal_cursor.close(); }
but I am not able to get the address which is stored.
Any help is appreciated. Thanks
If you don't have READ_CONTACTS permission, you'll only be able to get information from the Contacts.CONTENT_URI table, not specific info like phone or postal address.
If you'd like to get a phone, you can use the special phone-picker intent instead like:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.Phone.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
or to get a postal address:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(CommonDataKinds.StructuredPostal.CONTENT_TYPE);
startActivityForResult(intent, REQUEST_CODE);
See here: https://developer.android.com/guide/components/intents-common#PickContactDat
If you want to get both pieces of information, you'd need to ask the user for a READ_CONTACTS permissions for those queries to work.
I want to display the native android contacts list and then once the user picks a contact, i would like to move to the next native screen where it shows the details of the contact (like phone numbers, email, etc).
Now once the user selects either an email address or a phone number, I would then like to get that data back to my original activity and do further processing.
I know that I could use
startActivityForResult(new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACT);
and then myself parse the contact using getContentResolver().query() and then store each of the required fields. Then finally display on a separate activity as contact details and then once the user chooses any one of the phone numbers or emails, then i use that to do further processing.
I tried to do the following inside onActivityResult():
Uri contactData = data.getData();
Intent intent = new Intent(Intent.ACTION_PICK, contactData);
startActivityForResult(intent, REQUEST_CODE_PICK_CONTACT_DETAIL);
This displays the details of the contact but when the user selects a particular number, android calls it instead of passing it back to me in the previous activity.
But I would like to use the native screens if possible and it would require a lot less parsing and no extra activities/fragments.
Could someone suggest how to go about doing this?
Thanks
You can get contact details from phone contact doing this way
Uri uri = data.getData();
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor people = getActivity().getContentResolver().query(uri, projection, null, null, null);
int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
int type = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
people.moveToFirst();
do {
String name = people.getString(indexName);
String number = people.getString(indexNumber);
System.out.println(name+number);
} while (people.moveToNext());
This might help:
Uri contactData = data.getData();
ContentResolver cr = getContentResolver();
Cursor c = cr.query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone = c
.getString(c
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + id, null, null);
phones.moveToFirst();
String cNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(getApplicationContext(), cNumber,
Toast.LENGTH_SHORT).show();
String nameContact = c
.getString(c
.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
// contact number = cNumber
//contact name = nameContact
}
}
I query the phone's calllog into a ListView. So when the user long clicks an item, a dialog comes up with options, including "View contact". To be able to view the contact the intent needs the contact id.
My problem is that I not always get to see the right contact. I click on Peter, and Peter's contact sheet comes up. I click on Sarah, and Jason's contact sheet comes up.
I must have been using this code the wrong way. Please help.
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phone));
Cursor cursor = contentResolver.query(uri, new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
if(cursor!=null) {
while(cursor.moveToNext())
{
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid2 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid2));
startActivity(intent_contacts);
Maybe what I need is not the PhoneLookup._ID, but some other ID.
I also tried Felipe's answer here, same happens.
Edit:
on a HTC Desire HD (2.3.5) I get the proper contacts in 99% of the
cases.
on a ZTE Blade (2.2) I get the proper contacts in 60% of the cases.
on a Samsung Galaxy Ace (2.3.3) I get the proper contacts in 5% of the cases.
What the hell is going on???
After digging myself into theme, I found sven's help on googlegroups. The problem was that I was using a depracated intent. I read through many pages on the developer site, I must have missed it somehow.
I also post the full code.
contactid26 = null;
ContentResolver contentResolver = getContentResolver();
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phonenumintolist));
Cursor cursor =
contentResolver.query(
uri,
new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID},
null,
null,
null);
if(cursor!=null) {
while(cursor.moveToNext()){
String contactName = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME));
contactid26 = cursor.getString(cursor.getColumnIndexOrThrow(PhoneLookup._ID));
}
cursor.close();
}
if (contactid26 == null) {
Toast.makeText(DetailedCallHistory.this, "No contact found associated with this number", Toast.LENGTH_SHORT).show();
}
else {
Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(contactid26)));
//Intent intent_contacts = new Intent(Intent.ACTION_VIEW, Uri.parse("content://contacts/people/" + contactid26));
startActivity(intent_contacts);
}
Here in this case i am going to show you that how to get the ID, and
Name of any contact no, which you have enter and want to search, For
Example if you enter a no and want to search its Name and id then use
this code, it is working 100%, if you want further modification in
this, then you can tell me
Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phnno.getText().toString()));//phone no
String[] proj = new String[]
{///as we need only this colum from a table...
Contacts.DISPLAY_NAME,
Contacts._ID,
};
String id=null;
String sortOrder1 = StructuredPostal.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
Cursor crsr = getContentResolver().query(lookupUri,proj, null, null, sortOrder1);
while(crsr.moveToNext())
{
String name=crsr.getString(crsr.getColumnIndex(Contacts.DISPLAY_NAME));
id = crsr.getString(crsr.getColumnIndex(Contacts._ID));
Toast.makeText(this,"Name of this cntct is : " +name +"\n id is : "+id ,Toast.LENGTH_SHORT).show();
}
crsr.close();
I'm trying to call the contact picker, get the persons name, phone and e-mail into strings and send them to another activity using an intent. So far this works:
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
// ...
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Intent intent = new Intent(CurrentActivity.this, NewActivity.class);
intent.putExtra("name", name);
startActivityForResult(intent, 0);
}
}
}
But if i add in:
String number = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
it force closes
Maybe theres another way to get their number?
Phone Numbers
Phone numbers are stored in their own table and need to be queried separately. To query the phone number table use the URI stored in the SDK variable ContactsContract.CommonDataKinds.Phone.CONTENT_URI. Use a WHERE conditional to get the phone numbers for the specified contact.
if (Integer.parseInt(cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
// Do something with phones
}
pCur.close();
}
Perform a second query against the Android contacts SQLite database. The phone numbers are queried against the URI stored in ContactsContract.CommonDataKinds.Phone.CONTENT_URI. The contact ID is stored in the phone table as ContactsContract.CommonDataKinds.Phone.CONTACT_ID and the WHERE clause is used to limit the data returned.
Email Addresses
Querying email addresses is similar to phone numbers. A query must be performed to get email addresses from the database. Query the URI stored in ContactsContract.CommonDataKinds.Email.CONTENT_URI to query the email address table.
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
}
emailCur.close();
As with the phone query the field names for the email table are also stored under ContactsContract.CommonDataKinds. The email query is performed on the URI in ContactsContract.CommonDataKinds.Email.CONTENT_URI and the WHERE clause has to match the ContactsContract.CommonDataKinds.Email.CONTACT_ID field. Since multiple email addresses can be stored loop through the records returned in the Cursor.
More tutorials here
This method requires Android API version 5 or higher.
Building on the accepted answer, if you want to jump straight to the desired email address and not require the contacts permission use something like this:
private static final int REQUEST_CODE_EMAIL = 1;
void startSelectingEmail() {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Email.CONTENT_URI);
startActivityForResult(intent, REQUEST_CODE_EMAIL);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_CODE_EMAIL) {
Uri emailUri = data.getData();
Cursor emailCursor = getContext().getContentResolver().query(emailUri, null, null, null, null);
if (emailCursor != null) {
if (emailCursor.moveToFirst()) {
String email = emailCursor.getString(
emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCursor.getString(
emailCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Email.TYPE));
Log.d(TAG, "Email: " + emailType + " " + email);
}
emailCursor.close();
}
}
}
This doesn't require the contacts permission to read the email address like the double query methods above. It also makes it so that you do not need to write UI for the user to select the appropriate email address for contacts with multiple emails, the user selects a specific email in the Contacts app so you only get one result.
The cursor comes back with quite a few columns in addition to just email address like display name, though that has only been verified on a Nexus 5 running Android M.