Get contact information from contact id in Android

I want to get some contact information from my contact list. I have this code:

list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) { //HERE I WANT TO GET CONTACT DETAILS FROM THE ID PARAMETER Uri lookupUri = Uri.withAppendedPath(Contacts.CONTENT_LOOKUP_URI, Uri.encode(id)); Cursor c = getContentResolver().query(lookupUri, new String[]{Contacts.DISPLAY_NAME}, null,null,null); try { c.moveToFirst(); String displayName = c.getString(0); } finally { c.close(); } } 

But I get this exception: IllegalArgumentException, Invalid lookup id (when I call the request method from the cursor). Therefore, I do not know how to get a valid search id from a list of elements.

Any idea? thanks!

+4
source share
3 answers

Here id means A contact id for which you want to get contact details,

A simple code to get a phone number for a specific contact id is similar to

  // Build the Uri to query to table Uri myPhoneUri = Uri.withAppendedPath( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id); // Query the table Cursor phoneCursor = managedQuery( myPhoneUri, null, null, null, null); // Get the phone numbers from the contact for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) { // Get a phone number String phoneNumber = phoneCursor.getString(phoneCursor .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); sb.append("Phone: " + phoneNumber + "\n"); } } 

So, from your question, I should doubt the id parameter you are singing in your uri, just clear this. Also in my example id is a string type ...

I hope you understand this.

Update: Uri.encode(id) instead of a simple run in string format .

Thanks..

+9
source

Since managedQuery deprecated, you can also use

 Cursor phoneCursor = getContentResolver().query(myPhoneUri, null, null, null, null); 
+3
source
 Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); int columnIndex = arg2.getColumnIndex(ContactsContract.Contacts._ID); private void retriveContactImage(ImageView imageView, int columnIndex) { Bitmap photo = null; try { InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(context.getContentResolver(), ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, Long.valueOf(columnIndex))); if (inputStream != null) { photo = BitmapFactory.decodeStream(inputStream); inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } imageView.setImageBitmap(photo); } private ArrayList<String> retrieveContactNumbers(TextView textView, long contactId) { ArrayList<String> phoneNum = new ArrayList<String>(); Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId + "" }, null); if (cursor.getCount() >= 1) { while (cursor.moveToNext()) { // store the numbers in an array String str = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); if (str != null && str.trim().length() > 0) { phoneNum.add(str); } } } cursor.close(); textView.setText(phoneNum.get(0)); return phoneNum; } private void retrieveContactName(TextView textView, long contactId) { String contactName = null; Cursor cursor = context.getContentResolver().query(Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts.DISPLAY_NAME }, ContactsContract.Contacts._ID + " = ?", new String[] { String.valueOf(contactId) }, null); if (cursor.moveToFirst()) { contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); } cursor.close(); textView.setText(contactName); } 
0
source

Source: https://habr.com/ru/post/1393507/


All Articles