0

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.

go sgenq
  • 313
  • 3
  • 13
  • I am not sure what happened, but i recalled the steps i did while testing the app, i added the phone contact no to the contact list and then deleted it, that is when the problem started.I tested the app on other devices and it works perfectly fine. – go sgenq Jun 12 '16 at 15:32
  • Is the only one contact available in your contact book? If so, After deleting the number from your contact book, how can you get the contact data? – Prasad Jul 19 '16 at 15:43
  • Did you got it? I'm having a similar problem, was working and now it returns null. – Tim Autin Sep 02 '16 at 23:13

0 Answers0