Android - 有没有办法使用联系人选择器获取电子邮件地址?

发布于 2024-12-13 03:03:45 字数 2907 浏览 1 评论 0原文

我正在尝试使用联系人选择器获取所选联系人的电子邮件地址。

但它不起作用。

这是我的代码:-

@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");
        }
    }
}
}

这是堆栈跟踪:-

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

如果我尝试获取名称,那么它可以工作,但如果我尝试获取电子邮件,它会抛出异常。

任何帮助将不胜感激。

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

奢欲 2024-12-20 03:03:45

我认为您应该使用活动结果进行选择,而不是将其用作 URI。

看看这个答案:如何读取联系人Android 2.0

第一个代码块的底部是查询电子邮件地址的地方。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文