I need your help for getting my contact photos.
I receive all my contacts with a birthday. Now I have to tried the photo of the person like that:
...
while (contactCursor.moveToNext()) {
Contact c = Contact.createContact(contactCursor);
c.setPhoto(getContactPhoto(contentResolver, c.getId()));
contacts.add(c);
}
..
private static InputStream getContactPhoto(ContentResolver contentResolver, long contactId) {
Uri contactUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, ContactsContract.Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = contentResolver.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 new ByteArrayInputStream(data);
}
}
} finally {
cursor.close();
}
return null;
}
My problem is now that I'm not able to receive any photo of the contacts (cursos size = 0). On the phone (Xperia Z1 Compact) are definitely contact photos.
My aim is then to set the photo like that:
if(contact.getPhoto() != null) {
Bitmap cPhoto = BitmapFactory.decodeStream(contact.getPhoto());
holder.photo.setImageBitmap(cPhoto);
} else {
ShapeDrawable d = new ShapeDrawable(new OvalShape());
d.getPaint().setColor(Color.BLUE);
d.setBounds(10, 10, 20, 20);
holder.photo.setImageDrawable(d);
}
Thank you very much for your help.
try this
public void getContacts(ContentResolver cr) {
Cursor cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
null, null);
while (cursor.moveToNext()) {
String name = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String photoUri = cursor
.getString(cursor
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
friendsListitem = new FriendsListitem(name, photoUri, phoneNumber);
arrayListFriends.add(friendsListitem);
}
cursor.close();// close cursor
}
load image using Picasso
Picasso.with(getApplicationContext())
.load(Uri.parse(photoUri)).noFade()
.into(imageView);
Related
I have all contact log i want to get contact image attached to the phone number and if no contact image then unknow image.
How can I do it? I have tried many method but nothing is working with me.
This is the method in official documentation to retrieve contact image.
public InputStream openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri,
new String[] {Contacts.Photo.PHOTO}, null, null, null);
if (cursor == null) {
return null;
}
try {
if (cursor.moveToFirst()) {
byte[] data = cursor.getBlob(0);
if (data != null) {
return new ByteArrayInputStream(data);
}
}
} finally {
cursor.close();
}
return null;
}
Have a visit on this link link
I am currently passing contact name and number with an intent. Which works fine. I am however unable to find out how I can pass a contacts photo and assign it to an ImageView in activity.
I've searched around here and not found an easy answer to this at all.
In case it's neccessary, my method to get name and number of whomever is calling:
private String getContactName(String number) {
String name = null;
String[] projection = new String[]{
ContactsContract.PhoneLookup.DISPLAY_NAME,
ContactsContract.PhoneLookup._ID};
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
Cursor cursor = this.c.getContentResolver().query(contactUri, projection, null, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
}
cursor.close();
}
return name;
}
It gets passed to my activity:
Bundle bundle = new Bundle();
bundle.putString("Number", number);
bundle.putString("Name", this.getContactName(number));
intent.putExtras(bundle);
Received with
Bundle bundle = this.getIntent().getExtras();
if (bundle.getString("Name").isEmpty()) {
this.name.setText("Unknown Caller");
} else {
this.name.setText(bundle.getString("Name"));
}
if (bundle.getString("Number").isEmpty()) {
this.number.setText(bundle.getString("Number"));
} else {
this.number.setText(bundle.getString("Name"));
}
try this you will get byte[] of contact photo pass it to intent and decode in second activity
private byte[] queryContactImage(int imageDataRow) {
Cursor c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, new String[] {
ContactsContract.CommonDataKinds.Photo.PHOTO
}, ContactsContract.Data._ID + "=?", new String[] {
Integer.toString(imageDataRow)
}, null);
byte[] imageBytes = null;
if (c != null) {
if (c.moveToFirst()) {
imageBytes = c.getBlob(0);
}
c.close();
}
return imageBytes;
}
and in second activity
if (imagebyte != null) {
Bitmap bitimg2 = BitmapFactory.decodeByteArray(imagebyte, 0, imagebyte.length);
yourimageview.setImageBitmap(bitimg2);
}
hope this will help you
You can take contactID from this cursor or your code. This is my method which will give you contact photo, just pass contact ID in it:
public InputStream openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri,
new String[] {Contacts.Photo.PHOTO}, null, null, null);
if (cursor == null) {
return null;
}
try {
if (cursor.moveToFirst()) {
byte[] data = cursor.getBlob(0);
if (data != null) {
return new ByteArrayInputStream(data);
}
}
} finally {
cursor.close();
}
return null;
}
This code (within my CustomAdapter class) displays only the contact id based on the who sent me text messages and puts them into an ArrayList, then displays the list.
I have a ImageView called holder.photo next to each contact id. How would I go about displaying the contact's photo in the in the ImageView?
String folder = "content://sms/inbox/";
Uri mSmsQueryUri = Uri.parse(folder);
messages = new ArrayList<String>();
contactID = new ArrayList<String>();
SMS = new ArrayList<String>();
try {
c = context.getContentResolver().query(mSmsQueryUri,
new String[] { "_id", "address", "date", "body" },
null, null, null);
if (c == null) {
Log.i(TAG, "cursor is null. uri: " + mSmsQueryUri);
}
} catch (Exception e) {
//Log.e(TAG, e.getMessage());
} finally {
c.close();
}
c.moveToFirst();
while (c.moveToNext()) {
phoneNumber = c.getString(0);
contactID.add(phoneNumber);
}
holder.photo.?????
//contact will cycle through all names and display each in a listview.
holder.contact.setText(contactID.get(position);
Currently, my listview displays this:
android_icon-----John Doe
android_icon-----Jane Smith
android_icon-----Foo Barr
try this..
public void getContacts(ContentResolver cr) {
Cursor phones = cr.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 contactId = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Bitmap bitmap = loadContactPhoto(
getContentResolver(), Long.valueOf(contactId))
}
phones.close();
get Bitmap image
public static Bitmap loadContactPhoto(ContentResolver cr, long id) {
Uri uri = ContentUris.withAppendedId(
ContactsContract.Contacts.CONTENT_URI, id);
InputStream input = ContactsContract.Contacts
.openContactPhotoInputStream(cr, uri);
if (input == null) {
return null;
}
return BitmapFactory.decodeStream(input);
}
I have lookupkeys for contact, now I want to obtain Contact Images as Bitmap/InputStream using these lookupkeys. Android documentation helps getting Images but with contacts id not lookupkey.
public InputStream openPhoto(long contactId) {
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri photoUri = Uri.withAppendedPath(contactUri, Contacts.Photo.CONTENT_DIRECTORY);
Cursor cursor = getContentResolver().query(photoUri,
new String[] {Contacts.Photo.PHOTO}, null, null, null);
if (cursor == null) {
return null;
}
try {
if (cursor.moveToFirst()) {
byte[] data = cursor.getBlob(0);
if (data != null) {
return new ByteArrayInputStream(data);
}
}
} finally {
cursor.close();
}
return null;
}
Running from pillar to post but no help. Thanks
Edit Dated 04-09-2012
Tried as suggested by #Sreejith Krishnan R but received the following exceptions in logcat
09-04 03:16:26.359: E/AndroidRuntime(24008): java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/lookup/0r272-382C544E2C562C382C4E582C42.2649i11.1987r6285-382C544E2C562C382C4E582C42.1987r6440-382C544E2C562C382C4E582C42.2829r6475-382C544E2C562C382C4E582C42/photo, calling user: com.xyz, calling package:com.xyz
09-04 03:16:26.359: E/AndroidRuntime(24008): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
09-04 03:16:26.359: E/AndroidRuntime(24008): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
[Updated]
Now this can be used since API level 5
public Bitmap getAvatar(Context context, String lookupKey){
Uri uri = getDataUri(context, lookupKey);
if (uri == null){
return null;
}
Cursor cursor = context.getContentResolver().query(
uri,
new String[] {ContactsContract.Data.DATA15},
null,
null,
null
);
if (cursor == null){
return null;
}
try{
if (cursor.moveToFirst()){
byte [] bytes = cursor.getBlob(0);
InputStream inputStream = new ByteArrayInputStream(bytes);
return BitmapFactory.decodeStream(inputStream);
}
} finally {
cursor.close();
}
return null;
}
public Uri getDataUri(Context context, String lookupKey){
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey),
new String[] {ContactsContract.Contacts.PHOTO_ID},
null,
null,
null
);
if (cursor == null){
return null;
}
try {
if (cursor.moveToFirst()){
long id = cursor.getLong(0);
/**http://developer.android.com/reference/android/provider/ContactsContract.ContactsColumns.html#PHOTO_ID
* If PHOTO_ID is null, consult PHOTO_URI or PHOTO_THUMBNAIL_URI,
* which is a more generic mechanism for referencing the contact photo,
* especially for contacts returned by non-local directories (see ContactsContract.Directory).
*/
if (id == 0){
if (Build.VERSION.SDK_INT < 11){
return null;
}
return getPhotoThumbnailUri(context, lookupKey);
}
return ContentUris.withAppendedId(ContactsContract.Data.CONTENT_URI, id);
}
} finally {
cursor.close();
}
return null;
}
//Available only for API level 11+
public Uri getPhotoThumbnailUri(Context context, String lookupKey){
Cursor cursor = context.getContentResolver().query(
Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_LOOKUP_URI, lookupKey),
new String[] {ContactsContract.Contacts.PHOTO_THUMBNAIL_URI},
null,
null,
null
);
if (cursor == null){
return null;
}
try{
if (cursor.moveToFirst()){
return Uri.parse(cursor.getString(0));
}
} finally {
cursor.close();
}
return null;
}
It was pretty easy to get the Contact picture when querying the People.CONTENT_URI, with a simple
People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId)
because I knew the contact Id. Now I need to do the same thing after accesing the Call log. With:
String[] strFields = {
android.provider.CallLog.Calls.CACHED_NAME,
android.provider.CallLog.Calls.NUMBER,
};
String strUriCalls="content://call_log/calls";
Uri UriCalls = Uri.parse(strUriCalls);
Cursor cursorLog = this.getContentResolver().query(UriCalls, strFields, null, null, null);
I get the list from call log, but I can't find any way of linking this with the contact id needed to load the photo. The app works from api level 4+.
Any help is appreciated. Thank you.
The solution, as guided by Cristian below, that works for me is:
private long getContactIdFromNumber(String number) {
String[] projection = new String[]{Contacts.Phones.PERSON_ID};
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,Uri.encode(number));
Cursor c = getContentResolver().query(contactUri, projection, null, null, null);
if (c.moveToFirst()) {
long contactId=c.getLong(c.getColumnIndex(Contacts.Phones.PERSON_ID));
return contactId;
}
return -1;
}
Then, you must try to get the contact ID by using the queried call log fields. So, you can implement something like this:
private String getContactIdFromNumber(String number) {
String[] projection = new String[]{Contacts.Phones._ID};
Uri contactUri = Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL,
Uri.encode(number));
Cursor c = getContentResolver().query(contactUri, projection,
null, null, null);
if (c.moveToFirst()) {
String contactId=c.getString(c.getColumnIndex(Contacts.Phones._ID));
return contactId;
}
return null;
}
Then, you can use that contact ID to get the photo. Something like this in your case:
cursorLog.moveToFirst();
String number = cursorLog.getString(cursorLog.getColumnIndex(android.provider.CallLog.Calls.NUMBER));
contactId = getContactIdFromNumber(number)
People.loadContactPhoto(activity, ContentUris.withAppendedId(People.CONTENT_URI, contactId);
// blah blah blah
This one works fine for me..
private void contactPickedFromLog(Intent data) {
// TODO Auto-generated method stub
String contactNumber = data.getStringExtra(CONTACT_NUMBER);
Cursor cursor = getContentResolver().query(
Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
Uri.decode(contactNumber)),
new String[] { PhoneLookup._ID }, null, null, null);
if(cursor.moveToFirst()){
long contactId = cursor.getLong(0);
InputStream inputStream = Contacts.openContactPhotoInputStream(
getContentResolver(),
ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId));
if(inputStream!=null)
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
}
}
I'm doing it in this way:
ContentResolver cr=this.getContentResolver();
Cursor cc = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cc.moveToNext())
{
contactId = cc.getString(cc.getColumnIndex(ContactsContract.Contacts._ID));
Uri contactPhotoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.parseLong(contactId));
InputStream is=ContactsContract.Contacts.openContactPhotoInputStream(cr, contactPhotoUri);
//blah-blah
}
Try this ...
public Bitmap getPhoto(String phoneNumber) {
Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));
Uri photoUri = null;
ContentResolver cr = getContentResolver();
Cursor contact = cr.query(phoneUri,
new String[] { ContactsContract.Contacts._ID }, null, null, null);
if (contact.moveToFirst()) {
long userId = contact.getLong(contact.getColumnIndex(ContactsContract.Contacts._ID));
photoUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, userId);
}
else {
Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
return getCircleBitmap(defaultPhoto);
}
if (photoUri != null) {
InputStream input = ContactsContract.Contacts.openContactPhotoInputStream(
cr, photoUri);
if (input != null) {
return getCircleBitmap(BitmapFactory.decodeStream(input));
}
} else {
Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
return getCircleBitmap(defaultPhoto);
}
Bitmap defaultPhoto = BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture);
contact.close();
return defaultPhoto;
}
All above answer is correct.You can also get photo by this...
c.getString(c.getColumnIndex(CallLog.Calls.CACHED_PHOTO_URI));
work in SDK >=23
if you are working with minimum SDK...
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(num));
uri = ((phone_uri != null) ? Uri.parse(phone_uri) : uri);
Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
if (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
image_uri = cursor.getString(cursor.getColumnIndex(ContactsContract.PhoneLookup.PHOTO_URI));
Log.d(TAG, "name " + name + " id "+id+" image_uri "+ image_uri);
}
cursor.close();
}