Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7051453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:15:55+00:00 2026-05-28T03:15:55+00:00

In an activity of my Android application, I would like to open the content

  • 0

In an activity of my Android application, I would like to open the content picker. And when the user selects one of those contacts, there should be a new entry (an event with type “other”) that is inserted into the table ContactsContract.CommonDataKinds.Event.

Starting the contact picker intent is easy. But then one must get some data for the selected contact and create a new entry in the event table. This is the code I have so far, unfortunately it doesn’t work:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
            case CONTACT_PICKER_ID:  
            Uri contactData = data.getData();
            //String contactID = contactData.getLastPathSegment();
            // ADD A NEW BIRTHDAY FOR THE SELECTED CONTACT START
            ContentValues values = new ContentValues();
            values.put(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE);
            values.put(ContactsContract.CommonDataKinds.Event.TYPE, ContactsContract.CommonDataKinds.Event.TYPE_OTHER);
            values.put(ContactsContract.CommonDataKinds.Event.CONTACT_ID, 250);
            values.put(ContactsContract.CommonDataKinds.Event.START_DATE, "2012-12-12");
            Uri dataUri = getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);
            // ADD A NEW BIRTHDAY FOR THE SELECTED CONTACT END
            break;
        }
    }
}

This code requires the permission “WRITE_CONTACTS”.

The contact ID “250” is hard-coded. Of course, it should be retrieved from the intent data that is sent along with the contact picker’s result.

The code above terminates with a NullPointerException. Why is this so? And how do I get the contact’s id from the intent so that I can use it for inserting the new row?

Edit: Additionally, the line …

getContentResolver().insert(ContactsContract.Data.CONTENT_URI, values);

… throws an exception. What’s wrong there?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:15:56+00:00Added an answer on May 28, 2026 at 3:15 am

    Here’s how to do it, assuming this is how you called the contact picker:

    int CONTACT_PICKER_ID = 1001; //some arbitrary number that you choose
    Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivity(intent, CONTACT_PICKER_ID);
    

    The data.getData() call returns a lookup key that you have to query the contacts DB for to find the contact’s ID.

    if (resultCode == RESULT_OK) {  
        switch (requestCode) {  
            case CONTACT_PICKER_ID:  
                Cursor cursor =  managedQuery(data.getData(), null, null, null, null);
                cursor.moveToFirst();
                //get the contact's ID
                contactId = Cursor.getLong(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
    
    ...
    

    The contactId contains the ID you want.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to create an Android application with real-time monitoring functions. One monitoring
i am new programmer in android application i would like display multiple notifications one
In my android application i would like to know if an activty is displayed
My Android Application has only one activity called MainActivity . When I press the
I'm developing an Android application. On one activity I have a List with list
I would like to use tabs in my Android application and since TabActivity is
I'm developing an Android Application that has three very similar Activities. I would like
I would like to schedule a phone call on an android application. I currently
I would like to pass Bitmap or Drawable from one Activity to another, now
I'm working on C2DM notification for an Android Application and I'd like to open

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.