Problem saving contacts in text file using string - java

This My Code for Save Contact in external storage directory. But this code is a problem when I open the Contact.txt file on my phone, the last phone number stored on the phone is recorded in the Contact.txt file.
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
String phoneNo = null;
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("TAG", "Name: " + name);
Log.i("TAG", "Phone Number: " + phoneNo);
}
pCur.close();
File root = new File(Environment.getExternalStorageDirectory(), "cache");
if (!root.exists())
root.mkdirs();
try {
File fileContact = new File(root, "Contact.txt");
FileWriter writer = new FileWriter(fileContact);
writer.append("Name: " + name + " - " + "Phone Number: " + phoneNo);
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("TAG", "Game Over.");
}
}
}
if (cur != null) {
cur.close();
}
}

your phones loop here:
while (pCur.moveToNext()) {
phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i("TAG", "Name: " + name);
Log.i("TAG", "Phone Number: " + phoneNo);
}
is looping over all the contact's phones, but when the loop exits, phoneNo will contain just the last phone, you need to record all phones into an ArrayList or something like that, and put them all on the file.

Related

How To Remove Unique Values From a Cursor

Im working on a Application where im only supposed to display duplicate phone contacts to the user so they can delete the duplicates.
Duplicates
So far im able to display all contacts using the following code:
Main Activity:
private void showContacts() {
Cursor cursor = ContactHelper.getContactCursor(getContentResolver(),"");
String[] fields = new String[]{ContactsContract.Data.DISPLAY_NAME};
adapter =new SimpleCursorAdapter(this,android.R.layout.simple_list_item_multiple_choice,cursor,fields,new int[]{android.R.id.text1});
listView.setAdapter(adapter);
adapter.notifyDataSetChanged(); }
ContactHelper.GetContactsCursor function:
public static Cursor getContactCursor(ContentResolver contactHelper,
String startsWith) {
String[] projection = { ContactsContract.CommonDataKinds.Phone._ID,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor cur = null;
try {
if (startsWith != null && !startsWith.equals("")) {
cur = contactHelper.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " like \"" + startsWith + "%\"", null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " ASC");
} else {
cur = contactHelper.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection, null, null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " ASC");
}
cur.moveToFirst();
} catch (Exception e) {
e.printStackTrace();
}
return cur;
}
private static long getContactID(ContentResolver contactHelper,
String number) {
Uri contactUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String[] projection = { ContactsContract.PhoneLookup._ID };
Cursor cursor = null;
try {
cursor = contactHelper.query(contactUri, projection, null, null,
null);
if (cursor.moveToFirst()) {
int personID = cursor.getColumnIndex(ContactsContract.PhoneLookup._ID);
return cursor.getLong(personID);
}
return -1;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
cursor = null;
}
}
return -1; }
how do i filter the unique values from the cursor? and only keep the duplicate contacts according to the phone number if possible?
May be this will work
try {
if (startsWith != null && !startsWith.equals("")) {
cur = contactHelper.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " like \"" + startsWith + "%\""
+ " AND "
+ ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " IN (SELECT " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " as name FROM view_data GROUP BY " +ContactsContract.CommonDataKinds.Phone.NUMBER + " HAVING COUNT(name)>1)",
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " ASC");
} else {
cur = contactHelper.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
projection,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " IN (SELECT " + ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " as name FROM view_data GROUP BY " +ContactsContract.CommonDataKinds.Phone.NUMBER + " HAVING COUNT(name)>1)",
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " ASC");
}
cur.moveToFirst();
} catch (Exception e) {
e.printStackTrace();
}

How to access contact on Android for API 17

I want to be able to read from contacts to my on my Android application but it doesn't seem achievable for APIs below 23. I intend to use this for API 17. How can I achieve this?
This code should work in API5 and above, using the enum ContactsContract.CommonDataKinds you have access to all field.
ContentResolver resolver = context.getContentResolver();
Cursor contacts = null;
try {
contacts = resolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (contacts.moveToFirst()) {
do {
String contactId = contacts.getString(contacts.getColumnIndex(ContactsContract.Contacts._ID));
Cursor emails = null;
Cursor phones = null;
try {
emails = resolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID
+ " = " + contactId, null, null);
while (emails.moveToNext()) {
String email = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
object
}
phones = resolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (phones.moveToNext()) {
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String displayName = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phoneContact object
}
} finally {
if (emails != null) {
emails.close();
}
if (phones != null) {
phones.close();
}
}
} while (contacts.moveToNext());
}
} finally {
if (contacts != null) {
contacts.close();
}
}
Try this function. This will give you all the contacts saved in phone
private void getContactList() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if ((cur != null ? cur.getCount() : 0) > 0) {
while (cur != null && cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(
ContactsContract.Contacts.DISPLAY_NAME));
if (cur.getInt(cur.getColumnIndex(
ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
Cursor pCur = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phoneNo = pCur.getString(pCur.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.i(TAG, "Name: " + name);
Log.i(TAG, "Phone Number: " + phoneNo);
}
pCur.close();
}
}
}
if (cur != null) {
cur.close();
}
}

how to get email address from contact list?

how to get email address from contact list.below code I got number and name and display on list.but I want number,name and email address so pls check below code
ContentResolver contactResolver = context.getContentResolver();
Cursor c = contactResolver.query(ContactsContract.Contacts.CONTENT_URI, new String[] { ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME, ContactsContract.Contacts.HAS_PHONE_NUMBER }, null, null, null);
Cursor cursor = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
null, null, null);
if(cursor.getCount()>0)
while ( cursor.moveToNext()) {
String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
// String photoUri = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
// String lookupKey = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Log.e("TAG", " Name: " + displayName+"==>phone Number==>"+number);
contactNameArrayList.add(displayName);
contactNumberArrayList.add(number);
if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = contactResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { contactId }, null);
while (pCur.moveToNext())
{
String phone = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String type = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(context.getResources(), Integer.parseInt(type), "");
// Log.e("TAG", s + " phone: " + phone);
}
pCur.close();
}
Cursor emailCursor = contactResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
null, null, null);
while (emailCursor.moveToNext())
{
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
int type = emailCursor.getInt(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String s = (String) ContactsContract.CommonDataKinds.Email.getTypeLabel(context.getResources(), type, "");
Log.e("TAG", s + " email: " + email);
}
emailCursor.close();
} cursor.close();
Log.e(TAG,"contactNameArrayList sIZE==>"+contactNameArrayList.size()+"contactNumberArrayList size==>"+contactNumberArrayList.size()+"contactEmailAddressArrayList size==>"+contactEmailAddressArrayList.size());
}
I create three array for name and number and email id but above code I got only mobile and name.when I am add name and mobile no in array so simultaneously store email id particular mobile no in email id array.so pls help ...
You can get Email from contact using this
public List<String> getEmail(int contactId) {
List<String> emailStr = new ArrayList<String>();
ContentResolver cr = context.getContentResolver();
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{String.valueOf(contactId)}, null);
while (emailCur.moveToNext()) {
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
emailStr.add(email);
System.out.println("Email " + email );
}
emailCur.close();
return emailStr;
}
Use Following Code to get multiple numbers and email from one contact and all contacts from phonebook.
private void getContactsFromPhoneBook() {
String unique_id = ApplicationConstant.phone_id;
System.out.println("Unique_ID : " + unique_id);
Contact_To_Sync contact_to_sync = new Contact_To_Sync();
List<Contacts> AllContact_toSync = new ArrayList<>();
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
System.out.println("Contacts Count : " + cur.getCount());
if (cur.getCount() > 0) {
Contacts contacts = null;
List<String> Phones;
List<String> Emails;
while (cur.moveToNext()) {
contacts = new Contacts();
Phones = new ArrayList<>();
Emails = new ArrayList<>();
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String timeStamp = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP));
contacts.setContact_id(id);
contacts.setName(name);
System.out.println("name : " + name + ", ID : " + id + " Status : " + timeStamp);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
Phones.add(phone);
}
pCur.close();
// get email and type
Cursor emailCur = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
new String[]{id}, null);
while (emailCur.moveToNext()) {
// This would allow you get several email addresses
// if the email addresses were stored in an array
String email = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCur.getString(
emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Emails.add(email);
System.out.println("Email " + email + " Email Type : " + emailType);
}
emailCur.close();
contacts.setEmails(Emails);
contacts.setPhones(Phones);
AllContact_toSync.add(contacts);
}
contact_to_sync.setContacts(AllContact_toSync);
contact_to_sync.setPhone_device_name(unique_id);
if (!contact_to_sync.getContacts().isEmpty()) {
Sync_Contacts_To_Server(contact_to_sync);
}
}
}

get Home Location from a contact in Contacts List Android?

i use the following code for get all Contacts:
public void getContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Contact_Details dt = new Contact_Details(name, number, UIDD);
dataArray.add(dt);
}
pCur.close();
}
}
}
cur.close();
}
now i want to get the home location from Contacts. what can i do now. home location is shown in the following image.
use this code to retrieve address from contact
Cursor pCur2 = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur2.moveToNext())
{
String address = pCur2.getString(pCur2.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
Log.d("tag", "Address: "+address);
}
pCur2.close();
this retrieve both phone and address
public void getContacts() {
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext())
{
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
{
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur.moveToNext())
{
String number = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.d("tag", "PhoneNumber: "+number);
}
pCur.close();
//get address of the phone
Cursor pCur2 = cr.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
new String[]{id}, null);
while (pCur2.moveToNext())
{
String address = pCur2.getString(pCur2.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS));
Log.d("tag", "Address: "+address);
}
pCur2.close();
}
}
}
cur.close();
}

Android Contacts - Get Phone number

I'm trying to get all phone numbers of a contact in Android. My code looks like this:
ContentResolver cr = context.getContentResolver();
String[] projection = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
String selection = ContactsContract.Data.CONTACT_ID + "=" + contactId;
Cursor nameCur = cr.query(ContactsContract.Data.CONTENT_URI, projection, selection, null, null);
while (nameCur.moveToNext()) {
String contact = nameCur.getString(nameCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
In principle, it works but the variable "contact" sometimes has values like "null", "1", "4" or "phonenumber#whatsapp". How can I really only get the phone numbers without these stupid WhatsApp Id strings?
You can also check what is contact: phone number or something else:
public static boolean isPhoneNumberValid(CharSequence phone) {
return !(TextUtils.isEmpty(phone)) && Patterns.PHONE.matcher(phone).matches();
}
i use below code and its work, see this:
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI, null, null,
null, null);
cursor.moveToFirst();
// data = new String[cursor.getCount()][12];
if (cursor.getCount() > 0) {
do {
try {
contactId = cursor
.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Uri contactUri = ContentUris.withAppendedId(
Contacts.CONTENT_URI,
Long.parseLong(contactId));
Uri dataUri = Uri.withAppendedPath(contactUri,
Contacts.Data.CONTENT_DIRECTORY);
Cursor phones = getContentResolver()
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = " + contactId,
null, null);
if (phones.getCount() > 0) {
ContactClass info = new ContactClass();
info.setid(contactId);
try {
Cursor nameCursor = getContentResolver()
.query(dataUri,
null,
Data.MIMETYPE + "=?",
new String[] { StructuredName.CONTENT_ITEM_TYPE },
null);
nameCursor.moveToFirst();
do {
String firstName = nameCursor
.getString(nameCursor
.getColumnIndex(Data.DATA2));
String lastName = "";
String displayname = cursor
.getString(cursor
.getColumnIndex(Contacts.DISPLAY_NAME_ALTERNATIVE));
if (!firstName.equals(displayname)) {
lastName = nameCursor
.getString(nameCursor
.getColumnIndex(Data.DATA3));
}
if (firstName.equals(null)
&& lastName.equals(null)) {
info.setfirstname("unknown name");
} else if (firstName.equals(null)) {
info.setlastname(lastName);
} else if (lastName.equals(null)) {
info.setfirstname(firstName);
} else {
info.setfirstname(firstName);
info.setlastname(lastName);
}
} while (nameCursor.moveToNext());
nameCursor.close();
} catch (Exception e) {
}
}
phones.close();
}
catch (Exception t) {
}
} while (cursor.moveToNext());
cursor.close();

Categories