How to import phone contacts list from Android native code to C# script?

Hi,
I am able to retrieve android phone contacts using android native code. Now I want to import
all those contacts (having fields data contact id, display names, email id and bitmap image) into Unity C# Script MonoDevelopment environment…
But I’m not getting how to do this. :face_with_spiral_eyes:

I’m making Android native code function call as follows…
if(Application.platform == RuntimePlatform.Android)
{
AndroidJavaClass jc = new AndroidJavaClass(“com.unity3d.player.UnityPlayer”);
AndroidJavaObject jo = jc.GetStatic(“currentActivity”);
jo.Call(“launchAndroidActivity”);
}

And my Android native code is…

public ContactListItem[ ] mContactList;
public void launchAndroidActivity()
{
mContactList = ContactAccessor.getAllDeviceContacts(this);
}

And Class ContactAccessor.java is as…
public class ContactAccessor {

private static Activity mActivity;
private static ContactListItem[ ] contactList = null;

private static Uri mEmailUri = android.provider.Contacts.ContactMethods.CONTENT_URI;

public static class ContactListItem {
private Bitmap impProfile;
private String contactName;
private String contactEmail;

public Bitmap getImpProfile() {
return impProfile;
}

public void setImpProfile(Bitmap impProfile) {
this.impProfile = impProfile;
}

public String getContactName() {
return contactName;
}

public void setContactName(String contactName) {
this.contactName = contactName;
}

public String getContactEmail() {
return contactEmail;
}

public void setContactEmail(String contactEmail) {
this.contactEmail = contactEmail;
}

public void printLog() {
log("impProfile: " + impProfile.toString() + ", contactName: "

  • contactName + ", contactEmail: " + contactEmail);
    }
    }

public static void log(String text) {
Log.d(“@@ContactAccessor”, text);
}

public static ContactListItem[ ] getAllDeviceContacts(Activity activity) {

mActivity = activity;

String[ ] projection = new String[ ] { People._ID, People.DISPLAY_NAME,
People.PRIMARY_EMAIL_ID, People.Phones.NUMBER };
Cursor cursor = mActivity.managedQuery(People.CONTENT_URI, projection,
null, null, People.NAME + " ASC");
cursor.moveToFirst();

log("Total Indices: " + cursor.getCount());
contactList = new ContactListItem[cursor.getCount()];
int i = 0;

do {
contactList = new ContactListItem();
_ contactList*.setContactName(cursor.getString(cursor*_
* .getColumnIndex(People.DISPLAY_NAME)));
_
String emailId = cursor.getString(cursor*_
* .getColumnIndex(People.PRIMARY_EMAIL_ID));
_ contactList.setContactEmail(getEmail(emailId));
// retrieve the contact photo as a Bitmap*
* int idCol = cursor.getColumnIndex(People.ID);*
* long id = cursor.getLong(idCol);*

* Uri uri = ContentUris.withAppendedId(People.CONTENT_URI, id);
_ Bitmap bitmap = People.loadContactPhoto(mActivity.getBaseContext(),_
uri, R.drawable.app_icon, null);
_ // set it here in the ImageView*
contactList*.setImpProfile(bitmap);
contactList.printLog();
i++;
} while (cursor.moveToNext());*_

* return contactList;*
* }*
* private static String getEmail(String id) {*
* String email = “”;*
* String[ ] projection = new String[ ] { ContactMethods.ID,*
* ContactMethods.DATA, ContactMethods.KIND };
String[ ] variables = new String[ ] { id };
try {
Cursor myCursor = mActivity.managedQuery(mEmailUri, projection,_

“contact_methods.” + ContactMethods.ID + " = ?“
+ " AND contact_methods.kind = 1”, variables,*

* ContactMethods.DATA + " ASC");
email = getEmailColumnData(myCursor);
} catch (SQLiteException ex) {
Log.d(“@@DEBUG”, ex.getMessage());
}
return email;
}
private static String getEmailColumnData(Cursor cur) {
String email = “”;
if (cur.moveToFirst()) {
int emailColumn = cur.getColumnIndex(ContactMethods.DATA);
do {
// Get the field values*

* email = cur.getString(emailColumn);
} while (cur.moveToNext());
}
return email;
}
}
Please help me to import Contact list into the Unity code or return it to Unity Code…
:)*_

May be u can do like this. Create one .txt or well formed .xml file of ur contact list with your .java code and save it. After saving that file just read that file from your csharp code.

Hi,

I am not familiar with saving and loading data by .xml file.
Can you help me by providing patch of code or by giving any helpful link for it?
Actually I’m scaring about bitmap data :frowning:

Thanks

may be this old answer but sure will help other searchers
check the new asset i did in asset store

before i did this asset I did a lot of searches with no answer, like this post