The previous article tells you how to add android contacts for each detailed contact field programmatically. This article will show you how to read contacts fields out from android contacts and print them in the android monitor Logcat console.
1. How To Query Android Contacts.
- To get all the android contacts fields, we need to use the android contacts content provider as following to get the Cursor object.
ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.Data.CONTENT_URI, queryColumnArr, whereClauseBuf.toString(), null, null);
- But before you loop the cursor object to get detailed contact field values, you need the below code to verify that the cursor is not null and the cursor return columns.
if(cursor!=null && cursor.getCount() > 0) { .................... }
- Otherwise below code maybe throw cursor index out of bounds exception error.
cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID));
- Below is the error message text.
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0.
- The error is because although the cursor object is not null, it does not contain any columns of data.
2. Query Android Contacts Example.
- There is an android contact in the emulator, it is generated by our previous article. When you click the “LOAD ALL CONTACTS” button, it will display all the contact fields on the android studio Logcat console.
3. Get Android Contacts Programmatically Example Source Code.
- Read contacts need runtime permission, so you need to add the below permissions in the AndroidManifest.xml file. You can read Android Marshmallow Runtime Permission Example to learn more about android runtime permission.
<uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" />
- Below is the main activity java file ContactsOperationActivity.java.
package com.dev2qa.example.datasharing.contacts; import android.Manifest; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.content.pm.PackageManager; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.ContactsContract; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.dev2qa.example.R; import com.dev2qa.example.datasharing.contacts.dto.ContactDTO; import com.dev2qa.example.datasharing.contacts.dto.DataDTO; import java.util.ArrayList; import java.util.List; public class ContactsOperationActivity extends AppCompatActivity { private static final String TAG_ANDROID_CONTACTS = "ANDROID_CONTACTS"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contacts_operation); setTitle("dev2qa.com - Android Contacts Operate Example."); // Load all contacts, and print each contact as log debug info. Button loadButton = (Button)findViewById(R.id.contact_operate_load); loadButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(!hasPhoneContactsPermission(Manifest.permission.READ_CONTACTS)) { requestPermission(Manifest.permission.READ_CONTACTS); }else { getAllContacts(); Toast.makeText(ContactsOperationActivity.this, "Contact data has been printed in the android monitor log..", Toast.LENGTH_SHORT).show(); } } }); } /* Return all contacts and show each contact data in android monitor console as debug info. */ private List<ContactDTO> getAllContacts() { List<ContactDTO> ret = new ArrayList<ContactDTO>(); // Get all raw contacts id list. List<Integer> rawContactsIdList = getRawContactsIdList(); int contactListSize = rawContactsIdList.size(); ContentResolver contentResolver = getContentResolver(); // Loop in the raw contacts list. for(int i=0;i<contactListSize;i++) { // Get the raw contact id. Integer rawContactId = rawContactsIdList.get(i); Log.d(TAG_ANDROID_CONTACTS, "raw contact id : " + rawContactId.intValue()); // Data content uri (access data table. ) Uri dataContentUri = ContactsContract.Data.CONTENT_URI; // Build query columns name array. List<String> queryColumnList = new ArrayList<String>(); // ContactsContract.Data.CONTACT_ID = "contact_id"; queryColumnList.add(ContactsContract.Data.CONTACT_ID); // ContactsContract.Data.MIMETYPE = "mimetype"; queryColumnList.add(ContactsContract.Data.MIMETYPE); queryColumnList.add(ContactsContract.Data.DATA1); queryColumnList.add(ContactsContract.Data.DATA2); queryColumnList.add(ContactsContract.Data.DATA3); queryColumnList.add(ContactsContract.Data.DATA4); queryColumnList.add(ContactsContract.Data.DATA5); queryColumnList.add(ContactsContract.Data.DATA6); queryColumnList.add(ContactsContract.Data.DATA7); queryColumnList.add(ContactsContract.Data.DATA8); queryColumnList.add(ContactsContract.Data.DATA9); queryColumnList.add(ContactsContract.Data.DATA10); queryColumnList.add(ContactsContract.Data.DATA11); queryColumnList.add(ContactsContract.Data.DATA12); queryColumnList.add(ContactsContract.Data.DATA13); queryColumnList.add(ContactsContract.Data.DATA14); queryColumnList.add(ContactsContract.Data.DATA15); // Translate column name list to array. String queryColumnArr[] = queryColumnList.toArray(new String[queryColumnList.size()]); // Build query condition string. Query rows by contact id. StringBuffer whereClauseBuf = new StringBuffer(); whereClauseBuf.append(ContactsContract.Data.RAW_CONTACT_ID); whereClauseBuf.append("="); whereClauseBuf.append(rawContactId); // Query data table and return related contact data. Cursor cursor = contentResolver.query(dataContentUri, queryColumnArr, whereClauseBuf.toString(), null, null); /* If this cursor return database table row data. If do not check cursor.getCount() then it will throw error android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0. */ if(cursor!=null && cursor.getCount() > 0) { StringBuffer lineBuf = new StringBuffer(); cursor.moveToFirst(); lineBuf.append("Raw Contact Id : "); lineBuf.append(rawContactId); long contactId = cursor.getLong(cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID)); lineBuf.append(" , Contact Id : "); lineBuf.append(contactId); do{ // First get mimetype column value. String mimeType = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE)); lineBuf.append(" \r\n , MimeType : "); lineBuf.append(mimeType); List<String> dataValueList = getColumnValueByMimetype(cursor, mimeType); int dataValueListSize = dataValueList.size(); for(int j=0;j < dataValueListSize;j++) { String dataValue = dataValueList.get(j); lineBuf.append(" , "); lineBuf.append(dataValue); } }while(cursor.moveToNext()); Log.d(TAG_ANDROID_CONTACTS, lineBuf.toString()); } Log.d(TAG_ANDROID_CONTACTS, "========================================================================="); } return ret; } /* * Get email type related string format value. * */ private String getEmailTypeString(int dataType) { String ret = ""; if(ContactsContract.CommonDataKinds.Email.TYPE_HOME == dataType) { ret = "Home"; }else if(ContactsContract.CommonDataKinds.Email.TYPE_WORK==dataType) { ret = "Work"; } return ret; } /* * Get phone type related string format value. * */ private String getPhoneTypeString(int dataType) { String ret = ""; if(ContactsContract.CommonDataKinds.Phone.TYPE_HOME == dataType) { ret = "Home"; }else if(ContactsContract.CommonDataKinds.Phone.TYPE_WORK==dataType) { ret = "Work"; }else if(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE==dataType) { ret = "Mobile"; } return ret; } /* * Return data column value by mimetype column value. * Because for each mimetype there has not only one related value, * such as Organization.CONTENT_ITEM_TYPE need return company, department, title, job description etc. * So the return is a list string, each string for one column value. * */ private List<String> getColumnValueByMimetype(Cursor cursor, String mimeType) { List<String> ret = new ArrayList<String>(); switch (mimeType) { // Get email data. case ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE : // Email.ADDRESS == data1 String emailAddress = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS)); // Email.TYPE == data2 int emailType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); String emailTypeStr = getEmailTypeString(emailType); ret.add("Email Address : " + emailAddress); ret.add("Email Int Type : " + emailType); ret.add("Email String Type : " + emailTypeStr); break; // Get im data. case ContactsContract.CommonDataKinds.Im.CONTENT_ITEM_TYPE: // Im.PROTOCOL == data5 String imProtocol = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.PROTOCOL)); // Im.DATA == data1 String imId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Im.DATA)); ret.add("IM Protocol : " + imProtocol); ret.add("IM ID : " + imId); break; // Get nickname case ContactsContract.CommonDataKinds.Nickname.CONTENT_ITEM_TYPE: // Nickname.NAME == data1 String nickName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Nickname.NAME)); ret.add("Nick name : " + nickName); break; // Get organization data. case ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE: // Organization.COMPANY == data1 String company = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.COMPANY)); // Organization.DEPARTMENT == data5 String department = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.DEPARTMENT)); // Organization.TITLE == data4 String title = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.TITLE)); // Organization.JOB_DESCRIPTION == data6 String jobDescription = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.JOB_DESCRIPTION)); // Organization.OFFICE_LOCATION == data9 String officeLocation = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Organization.OFFICE_LOCATION)); ret.add("Company : " + company); ret.add("department : " + department); ret.add("Title : " + title); ret.add("Job Description : " + jobDescription); ret.add("Office Location : " + officeLocation); break; // Get phone number. case ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE: // Phone.NUMBER == data1 String phoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); // Phone.TYPE == data2 int phoneTypeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); String phoneTypeStr = getPhoneTypeString(phoneTypeInt); ret.add("Phone Number : " + phoneNumber); ret.add("Phone Type Integer : " + phoneTypeInt); ret.add("Phone Type String : " + phoneTypeStr); break; // Get sip address. case ContactsContract.CommonDataKinds.SipAddress.CONTENT_ITEM_TYPE: // SipAddress.SIP_ADDRESS == data1 String address = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.SIP_ADDRESS)); // SipAddress.TYPE == data2 int addressTypeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.SipAddress.TYPE)); String addressTypeStr = getEmailTypeString(addressTypeInt); ret.add("Address : " + address); ret.add("Address Type Integer : " + addressTypeInt); ret.add("Address Type String : " + addressTypeStr); break; // Get display name. case ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE: // StructuredName.DISPLAY_NAME == data1 String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME)); // StructuredName.GIVEN_NAME == data2 String givenName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME)); // StructuredName.FAMILY_NAME == data3 String familyName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME)); ret.add("Display Name : " + displayName); ret.add("Given Name : " + givenName); ret.add("Family Name : " + familyName); break; // Get postal address. case ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE: // StructuredPostal.COUNTRY == data10 String country = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY)); // StructuredPostal.CITY == data7 String city = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY)); // StructuredPostal.REGION == data8 String region = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION)); // StructuredPostal.STREET == data4 String street = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET)); // StructuredPostal.POSTCODE == data9 String postcode = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE)); // StructuredPostal.TYPE == data2 int postType = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE)); String postTypeStr = getEmailTypeString(postType); ret.add("Country : " + country); ret.add("City : " + city); ret.add("Region : " + region); ret.add("Street : " + street); ret.add("Postcode : " + postcode); ret.add("Post Type Integer : " + postType); ret.add("Post Type String : " + postTypeStr); break; // Get identity. case ContactsContract.CommonDataKinds.Identity.CONTENT_ITEM_TYPE: // Identity.IDENTITY == data1 String identity = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Identity.IDENTITY)); // Identity.NAMESPACE == data2 String namespace = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Identity.NAMESPACE)); ret.add("Identity : " + identity); ret.add("Identity Namespace : " + namespace); break; // Get photo. case ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE: // Photo.PHOTO == data15 String photo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO)); // Photo.PHOTO_FILE_ID == data14 String photoFileId = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO_FILE_ID)); ret.add("Photo : " + photo); ret.add("Photo File Id: " + photoFileId); break; // Get group membership. case ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE: // GroupMembership.GROUP_ROW_ID == data1 int groupId = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID)); ret.add("Group ID : " + groupId); break; // Get website. case ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE: // Website.URL == data1 String websiteUrl = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.URL)); // Website.TYPE == data2 int websiteTypeInt = cursor.getInt(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Website.TYPE)); String websiteTypeStr = getEmailTypeString(websiteTypeInt); ret.add("Website Url : " + websiteUrl); ret.add("Website Type Integer : " + websiteTypeInt); ret.add("Website Type String : " + websiteTypeStr); break; // Get note. case ContactsContract.CommonDataKinds.Note.CONTENT_ITEM_TYPE: // Note.NOTE == data1 String note = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Note.NOTE)); ret.add("Note : " + note); break; } return ret; } // Return all raw_contacts _id in a list. private List<Integer> getRawContactsIdList() { List<Integer> ret = new ArrayList<Integer>(); ContentResolver contentResolver = getContentResolver(); // Row contacts content uri( access raw_contacts table. ). Uri rawContactUri = ContactsContract.RawContacts.CONTENT_URI; // Return _id column in contacts raw_contacts table. String queryColumnArr[] = {ContactsContract.RawContacts._ID}; // Query raw_contacts table and return raw_contacts table _id. Cursor cursor = contentResolver.query(rawContactUri,queryColumnArr, null, null, null); if(cursor!=null) { cursor.moveToFirst(); do{ int idColumnIndex = cursor.getColumnIndex(ContactsContract.RawContacts._ID); int rawContactsId = cursor.getInt(idColumnIndex); ret.add(new Integer(rawContactsId)); }while(cursor.moveToNext()); } cursor.close(); return ret; } // Check whether user has phone contacts manipulation permission or not. private boolean hasPhoneContactsPermission(String permission) { boolean ret = false; // If android sdk version is bigger than 23 the need to check run time permission. if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // return phone read contacts permission grant status. int hasPermission = ContextCompat.checkSelfPermission(getApplicationContext(), permission); // If permission is granted then return true. if (hasPermission == PackageManager.PERMISSION_GRANTED) { ret = true; } }else { ret = true; } return ret; } // Request a runtime permission to app user. private void requestPermission(String permission) { String requestPermissionArray[] = {permission}; ActivityCompat.requestPermissions(this, requestPermissionArray, 1); } // After user select Allow or Deny button in request runtime permission dialog // , this method will be invoked. @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); int length = grantResults.length; if(length > 0) { int grantResult = grantResults[0]; if(grantResult == PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "You allowed permission, please click the button again.", Toast.LENGTH_LONG).show(); }else { Toast.makeText(getApplicationContext(), "You denied permission.", Toast.LENGTH_LONG).show(); } } } }
- ContactDTO.java
package com.dev2qa.example.datasharing.contacts.dto; import java.util.ArrayList; import java.util.List; public class ContactDTO { // Contact belong group fields. private long groupId; // Contacts id. private long contactId; // Raw contacts id. Has same value of contact id. private long rawContactId; // Contact structured name fields. private String displayName; private String givenName; private String familyName; // Contact nickname fields. private String nickName; // Contact organization fields. private String company; private String department; private String title; private String jobDescription; private String officeLocation; // Contact phone list. private List<DataDTO> phoneList = new ArrayList<DataDTO>(); // Contact email list private List<DataDTO> emailList = new ArrayList<DataDTO>(); // Contact address list. private List<DataDTO> addressList = new ArrayList<DataDTO>(); // Contact website list. private List<DataDTO> websiteList = new ArrayList<DataDTO>(); // Contact note. private String note; // Contact im list. private List<DataDTO> imList = new ArrayList<DataDTO>(); // Contact postal fields. private String country; private String city; private String postCode; private String street; private String region; private long postType; // Contact identity fields. // Identity value private String identity; // Identity card, passport etc. private String namespace; // Contact photo fields. private String photo; private String photoFieldId; public long getContactId() { return contactId; } public void setContactId(long contactId) { this.contactId = contactId; } public long getRawContactId() { return rawContactId; } public void setRawContactId(long rawContactId) { this.rawContactId = rawContactId; } public String getDisplayName() { return displayName; } public void setDisplayName(String displayName) { this.displayName = displayName; } public String getGivenName() { return givenName; } public void setGivenName(String givenName) { this.givenName = givenName; } public String getFamilyName() { return familyName; } public void setFamilyName(String familyName) { this.familyName = familyName; } public String getNickName() { return nickName; } public void setNickName(String nickName) { this.nickName = nickName; } public String getCompany() { return company; } public void setCompany(String company) { this.company = company; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public List<DataDTO> getPhoneList() { return phoneList; } public void setPhoneList(List<DataDTO> phoneList) { this.phoneList = phoneList; } public List<DataDTO> getEmailList() { return emailList; } public void setEmailList(List<DataDTO> emailList) { this.emailList = emailList; } public List<DataDTO> getAddressList() { return addressList; } public void setAddressList(List<DataDTO> addressList) { this.addressList = addressList; } public List<DataDTO> getWebsiteList() { return websiteList; } public void setWebsiteList(List<DataDTO> websiteList) { this.websiteList = websiteList; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } public String getJobDescription() { return jobDescription; } public void setJobDescription(String jobDescription) { this.jobDescription = jobDescription; } public String getOfficeLocation() { return officeLocation; } public void setOfficeLocation(String officeLocation) { this.officeLocation = officeLocation; } public List<DataDTO> getImList() { return imList; } public void setImList(List<DataDTO> imList) { this.imList = imList; } public String getCountry() { return country; } public void setCountry(String country) { this.country = country; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getRegion() { return region; } public void setRegion(String region) { this.region = region; } public String getIdentity() { return identity; } public void setIdentity(String identity) { this.identity = identity; } public String getNamespace() { return namespace; } public void setNamespace(String namespace) { this.namespace = namespace; } public String getPhoto() { return photo; } public void setPhoto(String photo) { this.photo = photo; } public String getPhotoFieldId() { return photoFieldId; } public void setPhotoFieldId(String photoFieldId) { this.photoFieldId = photoFieldId; } public long getGroupId() { return groupId; } public void setGroupId(long groupId) { this.groupId = groupId; } public String getPostCode() { return postCode; } public void setPostCode(String postCode) { this.postCode = postCode; } public long getPostType() { return postType; } public void setPostType(long postType) { this.postType = postType; } }
- Below is the activity layout XML file activity_contacts_operation.xml.
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <Button android:id="@+id/contact_operate_load" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Load All Contacts"/> <Button android:id="@+id/contact_operate_add" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Add Contact"/> <Button android:id="@+id/contact_operate_update" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Update Contact"/> <Button android:id="@+id/contact_operate_delete" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Delete Contact"/> </LinearLayout>
Reference
- How To Add Contact In Android Programmatically
- Android Contacts Database Structure
- Android Contacts Fields, Data Table Columns, And Data Mimetype Explain
- Android Marshmallow Runtime Permission Example
can u please share me sample ,in my android studio not working ,please help me i need similar contacts details.
Thanks and it was very useful for me.
I converted it to C#
Best regards
Below is DataDTO file source code.
package com.dev2qa.example.datasharing.contacts.dto;
/**
* Created by Jerry on 1/26/2018.
*/
public class DataDTO {
private int dataType;
private String dataValue;
public int getDataType() {
return dataType;
}
public void setDataType(int dataType) {
this.dataType = dataType;
}
public String getDataValue() {
return dataValue;
}
public void setDataValue(String dataValue) {
this.dataValue = dataValue;
}
}
I think i found why.
I made a change to the code at the where clause.
Instead of whereClauseBuf.append(ContactsContract.Data.RAW_CONTACT_ID);
i put this whereClauseBuf.append(ContactsContract.Data.CONTACT_ID);.
and of course i first retrieve the contact ids and not the raw contact ids.
I tried the code only on my phone. I dont know if it will work fine on another device.
It works for some contacts. For some contacts failed to fetch info because cursor.getCount() is zero.
I cannot understand why. The contact has phone numbers, email e.t.c.
String photo = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Photo.PHOTO));
causes exception:
android.database.sqlite.SQLiteException: unknown error (code 0): Unable to convert BLOB to string
Pro-Tip: to store a binary as a String you have to convert it to base64 😉
within getAllContacts(), where are you calling ret.add() ???
Does this sample code even work ?
The example code work.
But the getAllContacts() method just print added or exist contacts info in the logcat console.
You had better add below code to require WRITE_CONTACTS permission before add contacts.
else if(!hasPhoneContactsPermission(Manifest.permission.WRITE_CONTACTS))
{
requestPermission(Manifest.permission.WRITE_CONTACTS);
}
First add contacts then load and print them in logcat console.
could you please post the ContactDTO class, cheers !
I found it in your previous article, thanks.
https://www.dev2qa.com/how-to-add-contact-in-android-programmatically/
ContactDTO java class source code has been added in the article.
Dude Where is its XML file ??
Activity layout xml file has been added.