I am referring to the https://developer.android.com/reference/android/provider/ContactsContract.Contacts.Entity.html#CONTENT_DIRECTORY. And I want to use the ContactsContract.Contacts.Entity URI for better results. APi level is 11 and my device OS is 4.0. I am using below code. And below is the exception I am getting in logs.
Please advice in which way I can use the ContactsContract.Contacts.Entity API.
Caused by: java.lang.IllegalArgumentException: URI: content://com.android.contacts/contacts/entities
Cursor phones=null;
ContentResolver cr = getContentResolver();
Uri phoneuri = Contacts.CONTENT_URI;
Uri phone_uri = phoneuri.withAppendedPath(phoneuri, ContactsContract.Contacts.Entity.CONTENT_DIRECTORY);
phones = cr.query(phone_uri, null, null, null, null);
for(int i=0;i<phones.getCount();i++)
{
Log.e("int","f_name="+phones.getColumnName(i));
}
phones.close();
Use this code to get all contacts from android:
ContentResolver cr = getContentResolver();
String[] projection = { ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts._ID };
Cursor contacts = cr.query(ContactsContract.Contacts.CONTENT_URI,
projection, null, null, "UPPER("
+ ContactsContract.Contacts.DISPLAY_NAME + ") ASC");
startManagingCursor(contacts);
Hope this will help you.
int contactId = 111;//one contact's id
Uri contactsUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri entityUri = Uri.withAppendedPath(contactsUri, Contacts.Entity.CONTENT_DIRECTORY);
Cursor c = getContentResolver().query(entityUri,
null,null, null, null);
Related
ContentResolver cr = getContentResolver();
String[] projection = new String[]{"body", "ct_t", "_id", "address"};
Uri uri = Uri.parse("content://mms-sms/conversations/" + id);
Cursor c = cr.query(uri, projection, null, null, null);
This queries all messages from a specific conversation and I would like only the sent messages to display as I need to distinguish each party of the conversation.
Is there such a thing as a uri like this:
Uri uri2 = Uri.parse("content://mms-sms/conversations/" + id + "/sent/");
You'll need to add a couple of columns to your projection, and include a selection argument in the query() call.
final String[] projection = {"_id", "address", "body", "ct_t", "type", "msg_box"};
final String selection = "(type = 2 OR msg_box = 2)";
final Uri uri = Uri.parse("content://mms-sms/conversations/" + threadId);
Cursor c = getContentResolver().query(uri, projection, selection, null, null);
An SMS message will have a type value of 2 when it has been sent. An MMS message will have the same value, but in the msg_box column.
I'm currently using the following to retrieve contacts from the phone:
ContentResolver cr = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] columnName = new String[] {
ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.TYPE,
ContactsContract.CommonDataKinds.Phone.NUMBER};
Cursor contactsCursor = cr.query(uri, columnName, null, null, "display_name ASC");
Does anyone knows how to retrieve distinct phone numbers from Android phone?
I am implementing the method discussed here How to Read MMS Data in Android. Here is the code snippet:
ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Uri uri = Uri.parse("content://mms-sms/conversations/");
Cursor query = contentResolver.query(uri, projection, null, null, null);
When I try to get the data through the cursor, I do not get the IDs of the MMS messages. I only get the IDs of the SMS messages.
change your code as:
ContentResolver contentResolver = getContentResolver();
final String[] projection = new String[]{"*"};
Uri MMSSMS_FULL_CONVERSATION_URI = Uri.parse("content://mms-sms/conversations");
Uri uri = MMSSMS_FULL_CONVERSATION_URI.buildUpon().
appendQueryParameter("simple", "true").build();
Cursor query = contentResolver.query(uri, projection, null, null, null);
My code is right. It just needs this code to get the id of the messages
cursor.moveToFirst();
String address = cursor.getString(cursor.getColumnIndex("_id"));
Is there a way to get all device contacts (aggregated, not raw) with contact ID, fist/last name, and contact picture?
Currently I am using the code below but it does not return structured name:
private static final String CONTACTS_SORT_ORDER = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";
// all contacts
public final String[] columns = {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.PHOTO_THUMBNAIL_URI};
c = contentResolver.query(Contacts.CONTENT_URI, null, null, null, CONTACTS_SORT_ORDER);
Thanks
It is not possible to fetch the required data with one query. For each contact ID you'll have to query its Data directory:
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI, contactId);
Uri dataUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
Then you should run queries against this URI, filtering MIMETYPE by StructuredName.CONTENT_ITEM_TYPE to obtain StructuredName for the given ID.
Uri dataUri = Uri.withAppendedPath(contactUri, Contacts.Data.CONTENT_DIRECTORY);
Cursor nameCursor = getActivity().getContentResolver().query(
dataUri,
null,
Data.MIMETYPE+"=?",
new String[]{ StructuredName.CONTENT_ITEM_TYPE },
null);
I'm working on android application in which I take backup of all contact information and then restore, I retrieve all information of contact,
For example:
Display Name
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null,null, null, null)
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Phone Number
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
Similary
But I unable to get "Internet Call" value.
Kindly anyone tell in which class I will get information about Internet Call information.
Dont know if this is the best way, but it worked, I am fairly new new to android.
Uri uri = ContactsContract.Data.CONTENT_URI;
String[] projection = new String[] {
ContactsContract.Data._ID,
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS,
ContactsContract.CommonDataKinds.SipAddress.TYPE,
};
String selection =
ContactsContract.Data.MIMETYPE+" ='"
+ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE+"'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME+ " COLLATE LOCALIZED ASC";
Cursor cursor = managedQuery(uri, projection, selection, selectionArgs, sortOrder);
It seems that the phone number is stored in misc information data and you have search on the mime type.
HTH
g.
i have tested. i am able to get internet call value. try below code.
Uri uri = ContactsContract.Contacts.CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cur=cr.query(uri, null, null, null, sortOrder);
if(cur.getCount()>0){
while(cur.moveToNext()){
if(Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)))> 0) {
String internetWhere = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ?";
String[] internetWhereParams = new String[]{id,ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE};
Cursor internetCur = cr.query(ContactsContract.Data.CONTENT_URI, null, internetWhere, internetWhereParams, null);
if (internetCur.moveToFirst()) {
String internetCall = internetCur.getString(internetCur.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS));
Log.e(TAG, "internet Call: " + internetCall);
} internetCur.close();
}
}
} cur.close();