How to get URI contact information

I am working with Android Contact ContentProvider. I have a phone number and I need to get the URI of the contact photo associated with this phone number. How can i do this???

I know that I can get raw photo data and create an InputStream, but I don't need the input stream, I need a URI.

EDIT: I originally use the following code to get contact information

Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNo)); Cursor cursor = context.getContentResolver().query(uri, details, null, null, null); 
+9
android android-contentprovider
Oct 12 '11 at 9:50 a.m.
source share
5 answers

To get the conatct ID using a phone number, use the following code:

 import android.provider.ContactsContract.PhoneLookup; public String fetchContactIdFromPhoneNumber(String phoneNumber) { Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = this.getContentResolver().query(uri, new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup._ID }, null, null, null); String contactId = ""; if (cursor.moveToFirst()) { do { contactId = cursor.getString(cursor .getColumnIndex(PhoneLookup._ID)); } while (cursor.moveToNext()); } return contactId; } 

and use the contact identifier obtained to get the contatc photo URI. Use the following code to get the URI of photos:

 import android.provider.ContactsContract; import android.provider.ContactsContract.CommonDataKinds.Phone; public Uri getPhotoUri(long contactId) { ContentResolver contentResolver = getContentResolver(); try { Cursor cursor = contentResolver .query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.CONTACT_ID + "=" + contactId + " AND " + ContactsContract.Data.MIMETYPE + "='" + ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE + "'", null, null); if (cursor != null) { if (!cursor.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, contactId); return Uri.withAppendedPath(person, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); } 

Hope this helps.

+11
Oct 12 2018-11-11T00:
source share

This solution demonstrates how to get an image from a user contact and then display it in an ImageView .

 ImageView profile = (ImageView)findViewById(R.id.imageView1); Uri my_contact_Uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_URI, String.valueOf(Contact_Id)); InputStream photo_stream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),my_contact_Uri); BufferedInputStream buf =new BufferedInputStream(photo_stream); Bitmap my_btmp = BitmapFactory.decodeStream(buf); profile.setImageBitmap(my_btmp); 
+7
Nov 10
source share

Here's the documentation documentation for Android .

 Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId); return Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY); 
+2
Jun 05
source share

You can get PHOTO_URI on NUMBER just using the following code, also you can use _ID.

  public static String getContactPhoto(Context context, String phoneNumber) { ContentResolver cr = context.getContentResolver(); Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); Cursor cursor = cr.query(uri, new String[]{ContactsContract.PhoneLookup.PHOTO_URI}, null, null, null); if (cursor == null) { return null; } String contactImage= null; if (cursor.moveToFirst()) { contactImage= cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI)); } if (!cursor.isClosed()) { cursor.close(); } return contactImage; } 
+1
Sep 18 '17 at 10:58 on
source share
  final boolean IS_HONEYCOMB = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB; String phoneNumber = "+1 416 385 7805"; ContentResolver contentResolver = context.getContentResolver(); Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber)); String[] projection = new String[] { ContactsContract.Contacts._ID, ContactsContract.Contacts.LOOKUP_KEY, IS_HONEYCOMB ? ContactsContract.Contacts.PHOTO_THUMBNAIL_URI : ContactsContract.Contacts._ID, }; Cursor cursor = contentResolver.query( uri, projection, null, null, null); if (cursor != null && cursor.moveToNext()) { long contactId = cursor.getLong(0); String lookupKey = cursor.getString(1); String thumbnailUri = cursor.getString(2); cursor.close(); } 

So, if sdk is honeycomb or higher, you have a thumbnail uri of the contact. Or you can build uri to search like this:

 Uri uri = ContactsContract.Contacts.getLookupUri(contactId, lookupKey); 

PS If you already know the id contact and / or search key , you can build Uri from the line:

search: content://com.android.contacts/contacts/lookup/{lookup key}/{contact id} thumbnail: content://com.android.contacts/contacts/{contact id}/photo

Therefore, it is better to cache these values.

0
May 14 '16 at 10:30
source share



All Articles