Question

I have tried storing record in android phonebook with following code but failed. I am posting the code I've tried,

@Override
public void onClick(View v) {
    String nm, num;
    nm = t1.getText().toString();
    num = t2.getText().toString();
    ContentResolver c = getContentResolver();
    ContentValues record = new ContentValues();
    int iddd = TotalCount + 1;
    record.put("Contacts._ID", iddd);
    record.put("Contacts.DISPLAY_NAME", nm);
    record.put("Contacts.HAS_PHONE_NUMBER", 1);
    try {
        c.insert(ContactsContract.Contacts.CONTENT_URI, record);
        ContentValues record1 = new ContentValues();
        record1.put("CommonDataKinds.Phone.NUMBER", num);
        c.insert(CommonDataKinds.Phone.CONTENT_URI, record1);
        Toast.makeText(this, "Contact saved2", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(this, "Failed to save contact", Toast.LENGTH_LONG)
                .show();
    }
}
    This code when executed, gives an error. Failed to insert contact.
    Minimum sdk version is 8.
    Target sdk is 18.

    But don't know why it's causing this error.
Was it helpful?

Solution

You can try this code for storing contacts from your phone... by storing the contact phone in arrayList and the contact name in another arrayList :

=== Contacts.java ===

package com.showcontacts;

import java.util.ArrayList;

import android.app.Activity;

import android.content.ContentResolver;

import android.database.Cursor;

import android.os.Bundle;

import android.provider.ContactsContract;

import android.util.Log;

import android.view.Menu;

import android.view.View;

public class Contacts extends Activity {

String phoneNumber;

ArrayList<String> phones = new ArrayList<String>();
ArrayList<String> contact_name = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contacts);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.contacts, menu);
    return true;
}

/**** get all contacts ******/
public void getNumber(View v) {
    ContentResolver cr = getContentResolver();
    String name;
    Cursor cursor = cr.query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null,
            null, null);
    while (cursor.moveToNext()) {
        name = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        phoneNumber = cursor
                .getString(cursor
                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        phones.add(phoneNumber);
        contact_name.add(name);// fill the Arraylist
    }
    cursor.close();// close cursor

    for (int i = 0; i < phones.size(); i++) {
        Log.d(contact_name.get(i) + i, (phones.get(i)));

    }
}

}

** activity_contacts.xml **

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Contacts" >
<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="26dp"
    android:text="Button" 
     android:onClick="getNumber"
     />

** Dont forget to add permissions in your Manifest **

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Best of luck

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top