Steven Schoen
Steven Schoen

Reputation: 4376

Ask the user to pick a contact, and then be able to look up that contact's info later?

I'm making an app that will ask for an existing contact, then keep some kind of identifier for that contact, and finally be able to look up that contact's info (specifically, the name and picture) at a later time.

I've got the picker intent:

Intent contactIntent = new Intent(Intent.ACTION_PICK,
                    ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(contactIntent, 5);

and the result:

public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
    case (pickCode):
        if (resultCode == RESULT_OK) {
            Uri contactData = data.getData();

But I'm lost here. How do I "mark down" this contact so I can get to it later?

And once I do that, how do I get the contact picture? Although I can probably figure this out, but if there's some easy way that hooks into the first part, that'd be nice.

Upvotes: 1

Views: 56

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

You can store this contact uri in SharedPreferences for later use or for sharing between other components as:

Create SharedPreferences as:

 SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
                                             MODE_WORLD_READABLE);
 SharedPreferences.Editor prefsEditor = myPrefs.edit();
 prefsEditor.putString("contact_uri", contactData.toString());
 prefsEditor.commit();

and for retrieving uri from SharedPreferences later:

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs",
                                               MODE_WORLD_READABLE);
 String contact_uri = myPrefs.getString("contact_uri", "not found");
 Uri contactData = Uri.parse(contact_uri);

Upvotes: 2

Related Questions