选择联系人Java Android时获取电子邮件列索引

时间:2019-04-14 14:18:01

标签: java android android-contentprovider android-contacts contactscontract

我当前正在从电话通讯录中选择一个联系人,然后使用以下代码保存姓名:

ERROR:tornado.general:Uncaught exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/tornado/http1connection.py", line 691, in _server_request_loop
    ret = yield conn.read_response(request_delegate)
  File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 807, in run
    value = future.result()
  File "/usr/local/lib/python3.6/dist-packages/tornado/concurrent.py", line 209, in result
    raise_exc_info(self._exc_info)
  File "<string>", line 3, in raise_exc_info
  File "/usr/local/lib/python3.6/dist-packages/tornado/gen.py", line 812, in run
    yielded = self.gen.send(value)
  File "/usr/local/lib/python3.6/dist-packages/tornado/http1connection.py", line 243, in _read_message
    self.stream.set_close_callback(self._on_connection_close)
  File "/usr/local/lib/python3.6/dist-packages/tornado/iostream.py", line 391, in set_close_callback
    self._maybe_add_error_listener()
  File "/usr/local/lib/python3.6/dist-packages/tornado/iostream.py", line 880, in _maybe_add_error_listener
    self._add_io_state(ioloop.IOLoop.READ)
  File "/usr/local/lib/python3.6/dist-packages/tornado/iostream.py", line 910, in _add_io_state
    self.fileno(), self._handle_events, self._state)
  File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py", line 680, in add_handler
    self._impl.register(fd, events | self.ERROR)
OSError: [Errno 9] Bad file descriptor
^C> Disposing server...

我想知道是否有一种类似的方式来获取联系人的电子邮件,例如int nameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME); String name = cursor.getString(nameColumn)

1 个答案:

答案 0 :(得分:0)

  

从电话簿中选择联系人

表示您正在使用:

Intent intent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT)

让用户从设备的通讯录应用中选择联系人?

如果是这样,您可以稍微更改选择器代码,使其成为电子邮件选择器:

Intent intent = new Intent(Intent.ACTION_PICK, Email.CONTENT_URI);  
startActivityForResult(intent, PICK_EMAIL);

然后在您的onActivityResult中:

Uri emailUri = data.getData();
Cursor cursor = getContentResolver().query(emailUri, null, null, null, null);
String email = cursor.getString(cursor.getColumnIndex(Email.DATA)); // get the email itself

DatabaseUtils.dumpCursor(cursor); // dump the cursor so you can see the fields and data you can access
cursor.close();