Android : map contact photo with sms database android

on Friday, July 4, 2014


I am trying to build sms app and I have basic UI which shows contacts photo, name, sms time and text. here is the code :-



lv = (ListView) v.findViewById(R.id.listview);

final Uri inboxURI = Uri.parse("content://mms-sms/conversations/");

final String[] projection = new String[] { "*" };

cr = v.getContext().getContentResolver();
Cursor cursor = cr.query(inboxURI, projection, null, null,
Telephony.Sms.Inbox.DEFAULT_SORT_ORDER);
madapter = new ContactsViewAdapter(v.getContext(), R.layout.list_row,
cursor, new String[] { "body", "address", "date", "thread_id" },
new int[] { R.id.text_msg, R.id.phone_number, R.id.time_date,
R.id.thread_id_map }); //SimpleCursorAdapter
lv.setAdapter(madapter);


code to query the contact photo :-



public Uri getPhotoUri(Context ct, long contactId) {
ContentResolver contentResolver = ct.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);
}


but the problem is the contacts photos are not mapped with listview as its not there in SimpleCursorAdapter's cursor. So the pics show correctly but on the listview scroll the pics start showing at random position. How can i map the pics with SimpleCursorAdapter. Thanks


0 comments:

Post a Comment