I got this Exception
12-01 12:28:42.552: E/AndroidRuntime(25581): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
When I want to use below code to read phone(sim) contacts
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if(cursor.moveToFirst()&&cursor.getCount()>0&&cursor!=null){
while (cursor.moveToNext()) {
// String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phone = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
User user=new User();
user.setFirstname(name);
user.setPhoneNumber(phone);
listdata.add(user);
cla_contact.notifyDataSetChanged();
}
cursor.close();
}
What's [roblem and how to solve?
Replace your first line of code with the following it is working for me.
Cursor cursor =
getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null);
You better change the order of conditions in the 'if' loop to avoid null pointer exceptions (if in case cursor is null).
if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){
Please accept the answer if it works for you. Thank you.
Do like this
if (cursor.moveToFirst()) {
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println(contactID + " " + name);
}
}
it will move the cursor to the first row before reading the data.
Makr as right if it works for you. :)
Try this
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if(cursor.moveToFirst()){
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println(contactID + " " + name);
}
cursor.close();
}
and also make sure you have this in manifest
<uses-permission android:name="android.permission.READ_CONTACTS" />
try like this,
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
if(cursor != null && cursor.getCount() > 0){
if (cursor.moveToFirst()) {
do {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
System.out.println(contactID + " " + name);
} while (cursor.moveToNext());
}
cursor.close();
Related
I am making an app, in that app, the user will speak CALL (person name).
But, I am not able to understand how to access contacts and call that person automatically.
Till now, I am able to call a particular number only.
My code is mentioned below.
if(msg.indexOf("call")!=-1){
Intent i2=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+"123456789"));
startActivity(i2);
}
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if(cur!=null){
cur.close();
}
}
You can refer this stackoverflow links:
android get all contacts
http://saigeethamn.blogspot.com/2011/05/contacts-api-20-and-above-android.html
https://developer.android.com/training/contacts-provider/retrieve-names
You can use below library to fetch contacts
implementation 'com.github.tamir7.contacts:contacts:1.1.7'
https://github.com/tamir7/Contacts
Please, I've been trying all day to get the email address of all the contact on a phone but somehow, i can't get it to work. Please can someone tell me where am going wrong.. all others work except getting the email. Thanks in advance
Cursor cursor = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,null, null);
//now we have cusror with contacts and get diffrent value from cusror.
while (cursor.moveToNext()) {
String id = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
String name =cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
contactView.append("Name: ");
contactView.append(name);
contactView.append("\n");
if (Integer
.parseInt(cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactView.append("Number: ");
contactView.append(phoneNumber);
contactView.append("\n");}
//get email cursor
Cursor ecursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "+id,
null, null);
while (ecursor.moveToNext()) {
String email = ecursor.getString(ecursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
if(email != null){
contactView.append("email: ");
contactView.append(email);
contactView.append("\n");
}
}
ecursor.close();
}
cursor.close();
I have Tried number of available examples which helps to load contacts from phone. It works fine on emulator but when i try on real time mobile then it crashes. Can any one send me tested piece of code which is working flawless. then i can compare with my code.
one failed code example.
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
if (phones.getCount() > 0)
{
while (phones.moveToNext())
{
name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
Kindly help.
Be sure that phones is not null. If not null add this row to move pointer to 1st phone:
cursor.moveToFirst();
Here is snippets of code as example how it should work:
Uri contactData = data.getData();
//Cursor cursor = managedQuery(contactData, null, null, null, null);
Cursor cursor = cr.query(contactData, null, null, null, null);
cursor.moveToFirst();
String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
if (Integer.parseInt(cursor.getString(
cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id},
null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
break; // ? we want only 1 value
}
pCur.close();
}
cursor.close();
Suppose it will help you
First,add this in manifest file:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
to your AndroidManifest.xml file, then you can loop through your phone contacts like this:
Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(
ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (Boolean.parseBoolean(hasPhone)) {
// You know it has a number so now query it like this
Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null);
while (phones.moveToNext()) {
String phoneNumber = phones.getString(phones.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
}
phones.close();
}
Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null);
while (emails.moveToNext()) {
// This would allow you get several email addresses
String emailAddress = emails.getString(
emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
}
emails.close();
}
cursor.close();
Hi I am working in Android Contact search module.I am running below Query.
cur = context.getContentResolver().query(ContactsContract.Data.CONTENT_URI, null , null ,null, null);
from this query I am getting Result Multiple times.Is there any thing which I am doing wrong.I want DISTINCT Result Set.
please help me.
I think you mean you got duplicate record for some contacts. So you must add condition for your query
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
+ ("1") + "'";
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
+ " COLLATE LOCALIZED ASC";
cur = context.getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, projection, selection
+ " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ "=1", null, sortOrder);// this query only return contacts which had phone number and not duplicated
Try this code will help you
public void getContact() {
Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
ContentResolver contect_resolver = getContentResolver();
int size = cur.getCount();
if (size > 0 && cur != null) {
for (int i = 0; i < size; i++) {
cur.moveToPosition(i);
String id = cur.getString(cur.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String name = "";
Cursor phoneCur = contect_resolver.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[] { id }, null);
if (phoneCur.moveToFirst()) {
name = phoneCur.getString(phoneCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
if (!name.equalsIgnoreCase("")) {
String id1 = phoneCur.getString(phoneCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));
Cursor emails = getContentResolver()
.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + Integer.parseInt(id1),
null, null);
emailAddress="";
if (emails!=null && emails.getCount() > 0) {
emails.moveToFirst();
emailAddress = emails
.getString(emails
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DATA));
}
emails.close();
contact.setEmail(emailAddress);
id1 = "";
mcontact_arraylist.add(contact);
}
phoneCur.close();
}
}
cur.close();
}
}
Each record should contain a portion of the data for a contact (eg each phone number or address is a separate row) each row has a mimetype associated with it that is used to determine the data stored in each column. So for an address, the "data1" column holds the street data and data4 might hold the state.
i am trying to retrive all contact name and their number in my own list view. i am achive to get all name but when i am trying to get phone number too then it is going to display same number to me on every contact.
where number is getting value 1 from HAS_PHONE_NUMBER
my code is
if (number > 0) {
Cursor phones = managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID ,
null, null);
startManagingCursor(phones);
phones.moveToFirst();
String cNumber = phones.phones.getString(phones.getColumnIndex("data1"));
cache.nameView.setText(cache.nameBuffer.data, 0, size);
cache.numView.setText(cNumber);
}
Thanks in advance..
Try this:
//get all contacts
Cursor peopleCursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null,null, null);
if(peopleCursor.getCount()>0)
{
peopleCursor.moveToFirst();
for(int i=0;i<peopleCursor.getCount();i++)
{
if(check for HAS_PHONE_NUMBER)
{
//get number
Cursor numberCursor=getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},ContactsContract.CommonDataKinds.Phone._ID+"="+peopleCursor.getString(peopleCursor.getColumnIndex(ContactsContract.Contacts._ID)), null,null);
numberCursor.moveToFirst();
String number=numberCursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
//get name
String name=peopleCursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
peopleCursor.moveToNext();
}
}
}
u have to set While or for loop.In for code u used if basically it's used for condition that can't increment your value of number varable. count total number of name u fetch
it's variable name totalNumber_name
if (number == totalNumber_name) {
Cursor phones = managedQuery(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID ,
null, null);
startManagingCursor(phones);
phones.moveToFirst();
String cNumber = phones.phones.getString(phones.getColumnIndex("data1"));
cache.nameView.setText(cache.nameBuffer.data, 0, size);
cache.numView.setText(cNumber);
number++;
}
may be it's work
Try this code it work fine in my App.
while (c.moveToNext())
{
contactName = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
if (Integer.parseInt(c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactID },null);
while (pCur.moveToNext()) {
contactTelNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
}
Log.i("name ", contactName + " ");
Log.i("number ", contactTelNumber + " ");