Joining contacts and photo tables in Android - android

I am developing a phonebook app. I am trying to retrieve contact picture. At first I retrieved all the contacts and then took each contact_id and did a query on the photo table to get the picture. However, it is taking forever to query all the contacts for pics. As in my emulator there are more than 1000 contacts, so, more than 1000 hits on the photo table is being fired. So, is there a way to join the two tables and get the data in a single query?
Below is my code to do it. But I know its wrong. Just gave it a shot. Please someone correct it.
String[] projection = new String[]{Contacts._ID, Contacts.DISPLAY_NAME};
String joinCondition = "ContactsContract.Contacts._ID=ContactsContract.CommonDataKinds.Photo.CONTACT_ID";
ContentResolver cr = context.getContentResolver();
Uri contactUri = ContactsContract.Contacts.CONTENT_URI;
Uri fillUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cur = cr.query(contactUri, projection,
joinCondition, null, Contacts.DISPLAY_NAME);
Thx!
Rahul.

You should be retrieving the photo thumbnail in the list adapter's getView method. This is already optimized so that you only have to process the records which need to be shown on the screen. So at most you'll have to query a handful at a time, even if you have thousands of contacts.

you can try the following:
public final String[] columns = {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
Cursor c = contentResolver.query(Contacts.CONTENT_URI, columns, where, args, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
this should return you a cursor with all contacts and their pictures

Related

How to search contacts by name and phone number?

I am using Contacts.CONTENT_FILTER_URI to search for contacts.
Uri contentUri = Uri.withAppendedPath(
ContactsContract.Contacts.CONTENT_FILTER_URI,
Uri.encode(searchString));
The searchstring can be either a number or a name. That works great.
My only problem is that the result does not contain a contact phone number.
I know that I can get it by querying ContactsContract.Data.CONTENT_URI. However, I would like to find a solution that will give me a contact name and phone number with a single query.
You should use Phone.CONTENT_FILTER_URI instead of Contacts.CONTENT_FILTER_URI
Docs say:
The filter is applied to display names as well as phone numbers.
Try this:
Uri filterUri = Uri.withAppendedPath(Phone.CONTENT_FILTER_URI, Uri.encode(searchString));
String[] projection = new String[]{ Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER };
Cursor cur = getContentResolver().query(filterUri, projection, null, null, null);

How can I get all the raw contacts for a contact with a specific LOOKUP_KEY?

Given a phone number, I first find out the LOOKUP_KEY of the Contact that has this number. From there, is there a way to retrieve the list of raw contacts with a single query?
I tried the following approach
ContentResolver cr = context.getContentResolver();
// Get all RawContacts associated with lookupKey
Cursor contactCursor = cr.query(
Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookup_key),
new String[]{ContactsContract.RawContacts._ID},
null,
null,
null);
But contactCursor always contains a single entry, even when the queried contract has multiple raw contacts.
To be honest, I do not understand very well why the query above even returns anything, since there is no RawContacts entry in the table ContactsContract.Contacts. I guess that it just returns the _ID entry of this table, which is the Contact_ID (not the Id of a RawContact).
Don't use LOOKUP_KEY in this scenario, instead get the Contacts._ID from the phone number, and then get all RawContacts by CONTACT_ID:
cr.query(RawContacts.CONTENT_URI,
new String[]{ RawContacts._ID },
RawContacts.CONTACT_ID + "=" + theContactId,
null,
null);

Contacts are not sorting in Android

I have inserted some raw contacts (given account type and name null). the native contact app of android shows all contacts are sorted ( merged previews and newly given). But in my app (a listview for displaying contacts), it shows first the previous contatcs (sorted by display name) and then newly inserted contacs (also sorted). I have tried whatever combination possible , but no luck. please help any one.
Query Code
String PROJECTION[] = new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME};
private final String SORT_ORDER = ContactsContract.Contacts.DISPLAY_NAME + " ASC";
Uri uri = ContactsContract.Contacts.CONTENT_URI;
Cursor contacts = cr.query(uri, PROJECTION, null ,null, SORT_ORDER);
Update*strong text*
however , i was using handler , and now after converting to cusorloadr with loader manager. problem solved
Use getShort() method of Cursor to sort on the bases of a particular column.
try this as query :
Cursor cursor = getContentResolver.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, null ,null, Phone.DISPLAY_NAME + " ASC");

Android application contact groups

I need phone contacts to be divided into groups in my application, but only in my application so i don't want to add any groups to original phone contact database. I have made my own database to have my groups table:
groups(group_id int, group_name text, message text)
and contacts_group table.
contacts_group(contact_id, group_id)
Now the problem i am facing: How should i create the query to get all contacts from my group (from phone contact databse).
I need something like this: Select ...DISPLAY_NAME, ...NUMBER where ..._ID in (String[] ids) while String[] ids is an array of contact_ids from contacts_group table. Is it possible to put my string array in '?' in raw query? As.. Select... where .._ID in ?, string[] ?
Thanks for help in advance
Regards
Probelm solved. Solution:
public static Cursor getContactsFromGroup(String[] ids, SQLiteDatabase db) {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor c = context.getContentResolver().query(uri, projection,
"_ID IN (" + makePlaceHolders(ids.length) + ")", ids, null);
}
where makePlacHoldes(int) puts "?" chars, as many as long is param array.
Regards

Android getting contacts photo from data.Email query

I am making a autocomplete Field that queries contacts by Display name and Email. When someone clicks on the desired contact after the filtering that contact is added to a list with his email, display name and Picture if he has any.
So so far i have managed to do everything except to make the Photo appear. Here is how i run the query to get the email, display name, ID , and Photo ID.
return mContent.query(Email.CONTENT_URI,
PROJECTION, filter, null, null);
where projection is:
PROJECTION = new String[] {ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_ID,
Email.DATA
};
This one does what i need and returns all the data. But one thing i noticed during debugging this issue is that the contact id is different than if you run the query against ContactsContract.Contacts.CONTENT_URI for a specific display name for example.
For example the tests i have run where i get all the contacts by running the Contacts.CONTENT_URI gave me a contact with an image and Id of 152. However the query against the Email.CONTENT_URI gives me an id of 452 for the same contact (With same display name and email address). so when i try to get the Photo for a content uri containing the Id 452 it returns that the photo doesnt exist, but if i try to get the photo for 152 it works perfectly.
What is causing this issue? how do i get the correct User ID? Is there any relational query that i can maybe run to get a contact ID, or maybe a correct way to get it with the help of this one.
Thank you.
EDIT
I found this digging around old code. Might be helpful to anyone.
So the full query:
String[] PROJECTION = new String[] { ContactsContract.RawContacts._ID,
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.PHOTO_ID,
Email.DATA, ContactsContract.CommonDataKinds.Photo.CONTACT_ID };
String order = " CASE WHEN " + ContactsContract.Contacts.DISPLAY_NAME
+ " NOT LIKE '%#%' THEN 1" + " ELSE 2 END, "
+ ContactsContract.Contacts.DISPLAY_NAME + " COLLATE NOCASE";
String filter = Email.DATA + " NOT LIKE '' ) GROUP BY ( " + Email.DATA;
then its
getContentResolver().query( Email.CONTENT_URI, PROJECTION, filter, null, order);
When you want to access the photo of a contact, you need to specify the contact photo URI, for example using this method:
public Uri getContactPhotoUri(long contactId) {
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
photoUri = Uri.withAppendedPath(photoUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
return photoUri;
}
But for contactId you must use:
String id = ContactsContract.CommonDataKinds.Photo.CONTACT_ID;
long contactId = Long.parseLong(id);
Please note that a common error is to use ContactsContract.Contacts._ID instead ContactsContract.CommonDataKinds.Photo.CONTACT_ID
I hope that can help you.
You should use RAW_CONTACT_ID in the query. For ex, there can be two different contacts i.e. different RAW_CONTACT_ID for a single CONTACT_ID.
maybe you can take a look at this blog post in the example there they query all contacts, email addresses and the contact photo
http://blog.app-solut.com/2011/03/working-with-the-contactscontract-to-query-contacts-in-android/
best code will be
Uri photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Id);
Bitmap photoBitmap;
ContentResolver cr = getContentResolver();
InputStream is = ContactsContract.Contacts.openContactPhotoInputStream(cr, photoUri);
photoBitmap = BitmapFactory.decodeStream(is);
it works for all

Categories

Resources