0

I ma trying to fetch an email address of selected contact using contact picker.

But it isn't working.

Here is my code :-

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == RESULT_OK)
    {
        Uri contactData = data.getData();
        Log.e(TAG, contactData.toString());
        String[] PROJECTION = new String[] {ContactsContract.CommonDataKinds.Email.DATA};
        Cursor contactInfo = managedQuery(contactData, PROJECTION, null, null, null);
        if(contactInfo.moveToFirst())
        {
            String name = contactInfo.getString(0);
            Log.e(TAG, name);
        }
        else
        {
            Log.e(TAG, "No Data");
        }
    }
}
}

Here is a stack trace : -

10-31 23:47:29.912: ERROR/AndroidRuntime(1978): FATAL EXCEPTION: main
        java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1001, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-572D4F5547/1 flg=0x1 (has extras) }} to activity {com.varundroid.comm/com.varundroid.comm.InstantEmail}: java.lang.IllegalArgumentException: Invalid column data1
        at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
        at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
        at android.app.ActivityThread.access$2000(ActivityThread.java:117)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:3683)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.IllegalArgumentException: Invalid column data1
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:144)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
        at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
        at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
        at android.content.ContentResolver.query(ContentResolver.java:262)
        at android.app.Activity.managedQuery(Activity.java:1550)
        at com.varundroid.comm.InstantEmail.onActivityResult(InstantEmail.java:56)
        at android.app.Activity.dispatchActivityResult(Activity.java:3908)
        at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
        ... 11 more

If i am trying to fetch a name then its working but if i try to fetch an email, its throwing an exception.

Any help would be highly appreciated.

Thanks.

Varundroid
  • 9,135
  • 14
  • 63
  • 93

1 Answers1

1

I think you should be doing a selection with the activity result rather than using that as the URI.

Take a look at this answer: How to read contacts on Android 2.0

At the bottom of the first code-block is where they are querying for email address.

Community
  • 1
  • 1
howettl
  • 12,419
  • 13
  • 56
  • 91
  • Interesting that this accepted answer was downvoted 2 years later without a comment to explain why. – howettl Oct 02 '13 at 18:40