I need to get the name, email and phone number from each contact of my contacts list.
The problem is I can get name and phone number, but not the email. I'm getting the phone number instead of email.
Here is my code:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
while (phones.moveToNext()) {
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String email = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
ContactItem objContact = new ContactItem();
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
objContact.setEmail(email);
list.add(objContact);
}
phones.close();
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
//May want to get basic info here like name, phone
//Example:
//Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
//while (phones.moveToNext()) {
// String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
// Log.i("phone", phoneNumber);
//}
//phones.close();
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("emails", emailAddress);
}
emails.close();
}
cursor.close();
Source and Reference:
How to read contacts on Android 2.0
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
String phoneNumber = null;
String name = null;
String emailAddress = null;
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
Cursor cursor = null;
cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
// May want to get basic info here like name, phone
// Example:
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null, null);
while (phones.moveToNext()) {
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("phone", phoneNumber);
}
phones.close();
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,
null, null);
while (emails.moveToNext()) {
emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
Log.i("emails", emailAddress);
}
emails.close();
}
cursor.close();
Log.i(DEBUG_TAG, "Got a contact result: " + name + emailAddress + "Phone Number is :- " + phoneNumber);
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
}
Related
I am using
Uri uriSms = Uri.parse("content://mms-sms/conversations?simple=true");
Cursor cursor = getContentResolver().query(uriSms, null,null,null,null);
cursor.moveToLast();
while (cursor.moveToPrevious())
{
String recipient_ids= cursor.getString(cursor.getColumnIndex("recipient_ids"));
String body = cursor.getString(cursor.getColumnIndex("snippet"));
}
to get a list of sms conversation.
The "recipient_ids" returns some value Like 302, 301 259 etc. What I want is a function where i will pass the "recipient_ids" as perameter and it will return the display name of the contact (If available else the Number)
These methods help you to achieve what you want:
getContactByRecipientId - to get contact number by recipientId.
getContactbyPhoneNumber - to get display name by phone number.
public String getContactByRecipientId(long recipientId) {
String contact = "";
Uri uri = ContentUris.withAppendedId(Uri.parse("content://mms-sms/canonical-address"), recipientId);
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
try {
if (cursor.moveToFirst()) {
contact = getContactbyPhoneNumber(cursor.getString(0));
}
} finally {
cursor.close();
}
return contact;
}
public String getContactbyPhoneNumber(String phoneNumber) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
String[] projection = {ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup.NORMALIZED_NUMBER };
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
String name = null;
String nPhoneNumber = phoneNumber;
try {
if (cursor.moveToFirst()) {
nPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.NORMALIZED_NUMBER));
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
} finally {
cursor.close();
}
if(name != null){ // if there is a display name, then return that
return name;
}else{
return nPhoneNumber; // if there is not a display name, then return just phone number
}
}
Please check this code
private void fetchInbox() {
Uri inboxURI = Uri.parse("content://sms/inbox");
String[] reqCols = new String[]{"_id", "address", "body"};
ContentResolver cr = getContentResolver();
Cursor c = cr.query(inboxURI, reqCols, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.row, c,
new String[]{"body", "address"}, new int[]{
R.id.lblMsg, R.id.lblNumber}, 0);
}
I doing this
contactUri = ContactsContract.Contacts.CONTENT_URI;
Intent intentOpenContact = new Intent(Intent.ACTION_PICK, contactUri);
startActivityForResult(intentOpenContact, CONTACT_PICKER);
and launch the contact list and got the information I picked
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case CONTACT_PICKER:
String name = null;
int phoneIndex = -1;
int emailIndex = -1;
String phone = null;
String email = null;
try{
Uri contactUri = data.getData();
Cursor cursor = getContentResolver().query(contactUri, null, null, null, null);
if(cursor.moveToFirst())
name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = contactUri.getLastPathSegment();//the id you picked
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (cursorPhone != null && cursorPhone.moveToFirst()) {
phoneIndex = cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
}
Cursor cursorEmail = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
if (cursorPhone != null && cursorEmail.moveToFirst()) {
emailIndex = cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
}
if(phoneIndex != -1){
phone = cursorPhone.getString(phoneIndex);
}
if(emailIndex != -1){
email = cursorEmail.getString(emailIndex);
}
Log.d(TAG, "name: " + name + " ,phone: " + phone + " ,email: " + email);
} catch (Exception e){
Log.e(TAG, e.getMessage());
}
}}
}
But I wish to go the next page of contact person information, then get the information(email or phone) user clicked.
What can I do?
I have the following code to load all contacts with their pictures
public static void getAllContactWithNumberAndNameAndPhoto(Context context,
ArrayList<ContactInfo> mContactList, boolean starred) {
ContentResolver cr = context.getContentResolver();
Cursor cur = null;
if (starred == true) {
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"starred=?", new String[] { "1" }, null);
} else {
cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);
}
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
ContactInfo item = new ContactInfo();
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(cur
.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// Uri photo = PhoneUtils.getPhotoUriFromID(context, id);
String starredValue = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.STARRED));
boolean isFav = false;
if (starredValue.equals("1"))
isFav = true;
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()) {
String phoneNo = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
item.addPhone(removeCharactersFromPhoneNumber(phoneNo));
}
pCur.close();
// if (photo != null) {
//
// item.setPhoto(photo.toString());
// }
item.setName(name);
item.setFavorite(isFav);
item.setRecent(false);
mContactList.add(item);
}
}
cur.close();
}
}
and the following code to load contact picture Uri
public static Uri getPhotoUriFromID(Context context, String id) {
try {
Cursor cur = context
.getContentResolver()
.query(ContactsContract.Data.CONTENT_URI,
null,
ContactsContract.Data.CONTACT_ID
+ "="
+ id
+ " AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE
+ "'", null, null);
if (cur != null) {
if (!cur.moveToFirst()) {
return null;
}
} else {
return null;
}
cur.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
Uri person = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, Long.parseLong(id));
return Uri.withAppendedPath(person,
ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
}
every thing is working fine but too slow about 40 second for 1000 contact, when I comment the part of loading image it takes 18 sec, so how can I load image uri in the same query of loading contact to minimize the waiting time of the user.
For displaying the images of contacts you can use Universal Image Loader library. It's great for handling multiple images. You can use a default image to display initially and as soon as the contact's image is loaded the library will display that image.
This is how you display image in this library:
ImageLoader.getInstance().displayImage(img, imageView, options);
where img is the image, imageView is the ImageView where you want to display that image, options is the object of DisplayImageOptions of UniversalImageLoader library.
I'm trying to get name of Contact List from phone's contacts list, I have error on first line of try: ContentResolver cr = ContactFragment.getActivity().getContentResolver();
Any help?
ContactFragment.java
private String refreshData() {
String namedata = "";
try {
/**************************************************/
ContentResolver cr = ContactFragment.getActivity().getContentResolver();
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
"DISPLAY_NAME = '" + namedata + "'", null, null);
if (cursor.moveToFirst()) {
String contactId =
cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
//
// Get all phone numbers.
//
Cursor phones = cr.query(Phone.CONTENT_URI, null,
Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(namedata));
phones.close();
}
}
}
Try Like This :
Set this tag in manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS" />
Try this code :
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
int contactIdIdx = cursor.getColumnIndex(Phone._ID);
int nameIdx = cursor.getColumnIndex(Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
int photoIdIdx = cursor.getColumnIndex(Phone.PHOTO_ID);
cursor.moveToFirst();
do {
String idContact = cursor.getString(contactIdIdx);
String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
//...
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
I am trying to add the names of songs from an sd card on the phone to a list view but i am getting a null pointer exception...
private void updateList() {
String[] proj = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Artists.ARTIST };
Cursor tempCursor = managedQuery(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
proj,
null,
null,
null);
int col_index = -1;
int numSongs = tempCursor.getCount();
while (tempCursor.moveToNext()) {
col_index = tempCursor.getColumnIndexOrThrow(
MediaStore.Audio.Artists.ARTIST);
songname.add(tempCursor.getString(col_index));
}
ArrayAdapter<String> songss = new ArrayAdapter<String>(
this, R.id.songs, songname);
setListAdapter(songss);
}
I am just simply get songs data from sdcard as follows...It may helpful for you
Cursor cursor = getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
null);
if(cursor.moveToFirst())
{
for(int j=0;j<cursor.getCount();j++)
{
int ALBUM_ID = cursor.getInt((cursor
.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM_ID)));
String album_url = null;
Uri sArtworkUri = Uri
.parse("content://media/external/audio/albumart");
Uri uri = ContentUris.withAppendedId(sArtworkUri, ALBUM_ID);
album_url = uri.toString();
ContentResolver res = this.getContentResolver();
// Album
String album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.AlbumColumns.ALBUM));
String year = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.AudioColumns.YEAR));
// String year = cursor.getString(cursor
// .getColumnIndex(MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS));
// artist
String artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.ArtistColumns.ARTIST));
// displayname
String DisplayName = cursor.getString(cursor
.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME));
// title
String Title = cursor.getString(cursor
.getColumnIndex(MediaStore.MediaColumns.TITLE));
}