Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a Photo from a Contact

Tags:

android

Alright, I'm just trying to learn about using Contact information, but I'm a bit stuck. I would like to be able to display a picture for the contact. Using the following code that I have, how would I be able to put the photo for the contact in the ImageView in contact_entry?

ListView contacts_list = (ListView) findViewById(R.id.contacts_list);  // Gets the URI of the db Uri uri = ContactsContract.Contacts.CONTENT_URI; // What to grab from the db String[] projection = new String[] {         ContactsContract.Contacts._ID,         ContactsContract.Contacts.DISPLAY_NAME,         ContactsContract.Contacts.PHOTO_ID };  String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";  Cursor cursor = managedQuery(uri, projection, null, null, sortOrder);  String[] fields = new String[] {         ContactsContract.Data.DISPLAY_NAME };  int[] values = {          R.id.contactEntryText };  SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.contact_entry, cursor,         fields, values); contacts_list.setAdapter(adapter); 

contact_entry.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="54px">     <ImageView         android:id="@+id/contactPhoto"         android:layout_width="wrap_content"         android:layout_height="wrap_content"          android:src="@drawable/ic_contact_picture_3"/>     <TextView          android:text="@+id/contactEntryText"         android:id="@+id/contactEntryText"         android:layout_width="fill_parent"         android:layout_height="wrap_content"/> </LinearLayout> 
like image 1000
Chiggins Avatar asked Aug 18 '10 05:08

Chiggins


People also ask

Why do some contacts have a photo?

Some people add their own profile pics on their Google account. So anyone who has their email address (or number, if it's updated on their Google profile) will be detected by Google and linked to your account. For example if I'm your contact and I updated a profile pic, my pic will be updated on your phone.

Where are iPhone contact photos stored?

Answer: A: The image files for the contacts pictures are stored in the Images folder inside your Contacts account folder. The storage location is in your user Library/Application Support/AddressBook folder. You can get there by pasting this path into Go To Folder in the Finder (Go menu).

Where does iPhone pull contact photos from?

Your initial guess that it's Google Contacts is right - if you've set it up to sync with Google Contacts, it'll pull down the same contact photos as you have in Google Contacts, and it works both ways (assign a photo on iPhone, syncs back up to Google Contacts).


2 Answers

Probably this will help you(contact is identified by getId()):

/**  * @return the photo URI  */ public Uri getPhotoUri() {     try {         Cursor cur = this.ctx.getContentResolver().query(                 ContactsContract.Data.CONTENT_URI,                 null,                 ContactsContract.Data.CONTACT_ID + "=" + this.getId() + " AND "                         + ContactsContract.Data.MIMETYPE + "='"                         + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null,                 null);         if (cur != null) {             if (!cur.moveToFirst()) {                 return null; // no photo             }         } else {             return null; // error in cursor process         }     } catch (Exception e) {         e.printStackTrace();         return null;     }     Uri person = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long             .parseLong(getId()));     return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); } 

Usage is:

Uri u = objItem.getPhotoUri(); if (u != null) {         mPhotoView.setImageURI(u); } else {         mPhotoView.setImageResource(R.drawable.ic_contact_picture_2); } 
like image 187
Pentium10 Avatar answered Sep 19 '22 17:09

Pentium10


Android documentation says, that we should do it in this way.

public Bitmap openPhoto(long contactId) {         Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);         Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);         Cursor cursor = getContentResolver().query(photoUri,                 new String[] {ContactsContract.Contacts.Photo.PHOTO}, null, null, null);         if (cursor == null) {             return null;         }         try {             if (cursor.moveToFirst()) {                 byte[] data = cursor.getBlob(0);                 if (data != null) {                     return BitmapFactory.decodeStream(new ByteArrayInputStream(data));                 }             }         } finally {             cursor.close();         }         return null;  } 

For contactId you can use:

 public static long getContactIDFromNumber(String contactNumber, Context context) {     String UriContactNumber = Uri.encode(contactNumber);     long phoneContactID = new Random().nextInt();     Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, UriContactNumber),             new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME, ContactsContract.PhoneLookup._ID}, null, null, null);     while (contactLookupCursor.moveToNext()) {         phoneContactID = contactLookupCursor.getLong(contactLookupCursor.getColumnIndexOrThrow(ContactsContract.PhoneLookup._ID));     }     contactLookupCursor.close();      return phoneContactID; } 

Source: https://developer.android.com/reference/android/provider/ContactsContract.Contacts.Photo.html

like image 34
Andrii Kovalchuk Avatar answered Sep 20 '22 17:09

Andrii Kovalchuk