Calling an Activity from Main Activity , the Activity call CONTENT_URI through Intent
Intent intent=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
OnActivityResult i am trying to get id,contact name and phone but the Query Cursor was returning null
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
ContentResolver cr = getContentResolver();
String cNumber="";
switch (requestCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = cr.query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones =cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
Log.i("name",name);
Log.i("nameno", cNumber);
// arrayList.clear();
for(int i=0;i<arrayList.size(); i++)
{
String fromlist=arrayList.get(i).toString();
String addedtolist=name +"-" + cNumber;
if(fromlist.equals(addedtolist))
{
Toast.makeText(this, "Conatct already exists. ", Toast.LENGTH_LONG).show();
isExists=1;
}
}
if(isExists==0)
{
arrayList.add(name + "-" + cNumber);
lstContacts.setAdapter(arrayAdapter);
arrayAdapter.notifyDataSetChanged();
writeToFile(name + "-" + cNumber, "_ShareConatcts.txt");
sharecontact(android_id,name,cNumber,1);
}
isExists=0;
}
}
break;
}
}
The above command was working fine couple of days back but suddenly started giving null, it does not even throw any error. While debugging cursor C returns null.