LFG
LFG

Reputation: 33

Trying to access contacts within android app keep crashing

I am trying to access the contacts through my app. So I am able to share information through mail to a friend. So far the contacts are shown but when I click on a name to add to the email the application just go back to the previous screen and the email is not sent. The edit text is not set to the contact which I have chosen.

Android manifest

 <uses-permission android:name="android.permission.READ_CONTACTS"> </uses-permission>

EmailSend class

contacts.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent contactPickerIntent = new Intent(Intent.ACTION_PICK,
                    Contacts.CONTENT_URI);
            startActivityForResult(contactPickerIntent, CONTACT_PICKER_RESULT);
        }
    });

....

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
         System.out.println("yo");
        switch (requestCode) {
        case CONTACT_PICKER_RESULT:
            Cursor cursor = null;
            String email = "";
            System.out.println("Hello");
            try {
                Uri result = data.getData();
                Log.v("DEBUG: ", "Got a contact result: "
                        + result.toString());

                // get the contact id from the Uri
                String id = result.getLastPathSegment();

                // query for everything email
                cursor = getContentResolver().query(Email.CONTENT_URI,
                        null, Email.CONTACT_ID + "=?", new String[] { id },
                        null);

                int emailIdx = cursor.getColumnIndex(Email.DATA);

                // let's just get the first email
                if (cursor.moveToFirst()) {
                    email = cursor.getString(emailIdx);
                    Log.v("DEBUG: ", "Got email: " + email);
                } else {
                    Log.w("DEBUG: ", "No results");
                }
            } catch (Exception e) {
                Log.e("DEBUG: ", "Failed to get email data", e);
            } finally {
                if (cursor != null) {
                  cursor.close();
                }
               // EditText emailEntry = (EditText) findViewById(R.id.invite_email);
                personsEmail.setText(email);
                if (email.length() == 0) {
                    Toast.makeText(this, "No email found for contact.",
                            Toast.LENGTH_LONG).show();
                }

            }

            break;
        }

    } else {
        Log.w("DEBUG: ", "Warning: activity result not ok");
    }
}

What is more puzzling is that there is nothing that appears on logcat! So it surpasses all my error checks. Any idea why the screen crashes and the email address is not added to the EditText?

Upvotes: 2

Views: 424

Answers (2)

Rohit Heera
Rohit Heera

Reputation: 2727

Try this

public void getNumber(ContentResolver cr) {
        Cursor phones = cr.query(
                ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
                null, null);
        while (phones.moveToNext()) {
            ContactSynRequestcontacts data = new ContactSynRequestcontacts();

String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));

Cursor emails = contResv.query(Email.CONTENT_URI,null,Email.CONTACT_ID + " = " + contactId , null, null); while (emails.moveToNext()) { String email = emails.getString(emails.getColumnIndex(Email.DATA)); break; } emails.close();

            data.setName(name);
            data.setPhoneNumber(phoneNumber);
            userContactList.add(data);
        }
        phones.close();// close cursor
    }

    public class ContactSynRequestcontacts  {
    private String Name;

    public String getName() {
        return Name;
    }

    public void setName(String name) {
        this.Name = name;
    }
    private String PhoneNumber;

    public String getPhoneNumber() {
        return PhoneNumber;
    }

    public void setPhoneNumber(String mobile) {
        this.PhoneNumber = mobile;
    }
}

Upvotes: 0

frgr
frgr

Reputation: 65

In case you are testing it in the latest OS, that means Android M, they have introduced new set of permissions, known as run-time permissions.

http://developer.android.com/training/permissions/requesting.html

Check this link out and let us know if this solves the issue.

Upvotes: 1

Related Questions