3

I am trying to write and then to update contacts using URI content://com.android.contacts/contacts and I get a RunTime Error: Uid 10101 does not have permission to uri content://com.android.contacts/contacts. I declared permission in the Manifest file.

Here's the code

public class MainActivity extends Activity {
protected String[] names = {"Anda", "Candy", "Dandy"};
protected String[] numbers = {"12345678900","14035567890","12392344556"};
protected int[] ids = {1,2,3};

TextView txt;
ListView list;
ContentValues cv;
Uri u;

@Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txt = (TextView)findViewById(R.id.txt);
    list = (ListView) findViewById(R.id.list);
    Uri uri = getTheUri();
    grantUriPermission("ContactsContract.CommonDataKinds.Phone.CONTENT_URI", uri, 2);
    insertContacts(names, numbers, ids);
    showContacts();
    updateContact();
    showContacts();

}

public static Uri getTheUri() {
    //return Uri.parse("content://com.android.contacts/data/phones");
    return Uri.parse("content://com.android.contacts/contacts");
    //return Uri.parse("content://contacts/people");
}
protected void insertContacts(String[] names, String[] numbers, int[] ids) {
    ContentValues cv=new ContentValues();
    this.grantUriPermission("com.example.contactsdemo",Uri.parse("content://com.android.contacts/contacts"), MODE_WORLD_WRITEABLE);
    for (int i = 0; i < names.length; i++) {
        cv.put(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,names[i]);
        cv.put(ContactsContract.CommonDataKinds.Phone.NUMBER,numbers[i]);
        cv.put(ContactsContract.CommonDataKinds.Phone._ID,ids[i]);
        u = getContentResolver().insert(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, cv);
    }
}

protected void updateContact() {
    //update phone number 
    getContentResolver().update(u, cv, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + "=Anda", new String[]{"18585541315"});
}

protected void showContacts() {
    String [] projection=new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
            ContactsContract.CommonDataKinds.Phone.NUMBER,
            ContactsContract.CommonDataKinds.Phone._ID};
    int[] to = new int[] { R.id.txtName, R.id.txtNumber };
    txt.setText(ContactsContract.PhoneLookup.CONTENT_FILTER_URI.toString());
    Uri contacts = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    Cursor managedCursor = managedQuery(contacts,projection,null,null,null);
            //Cursor managedCursor =cr.query(contacts, projection, null, null, null);
    ListAdapter sca=new SimpleCursorAdapter(this,R.layout.list_entry,managedCursor,projection,to);
    list.setAdapter(sca);
}

}

AND HERE"S THE MANIFEST FILE:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.contactsdemo"
android:versionCode="1"
android:versionName="1.0" >

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

<uses-permission
            android:name="android.permission.READ_CONTACTS" />
<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:debuggable="true" >


    <activity
        android:name="com.example.contactsdemo.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

</application>


</manifest>

How do I get rid of the RunTime Error? Any help would be greatly appreciated.

Monica
  • 389
  • 1
  • 7
  • 19

3 Answers3

3

you are using spermission that is not meaning try to change the permission in your menifest

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

Full set of permissions flags can be found on Android reference pages.

Sunil Kumar
  • 7,086
  • 4
  • 32
  • 50
0

Check your MANIFEST file..

try adding like this..

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

it is uses-permission not uses-spermission...

Hope this helps...

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
  • Thank you for pointing out that mistake. Now I don't get anymore errors, but I don't find the contacts I've added through the program in the contacts list of my phone. – Monica Jun 22 '13 at 06:19
  • 06-21 23:34:03.389: E/AndroidRuntime(21016): FATAL EXCEPTION: main 06-21 23:34:03.389: E/AndroidRuntime(21016): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactsdemo/com.example.contactsdemo.MainActivity}: java.lang.UnsupportedOperationException: URI: content://com.android.contacts, calling user: com.example.contactsdemo, calling package:com.example.contactsdemo – Monica Jun 22 '13 at 06:39
  • u = getContentResolver().insert(ContactsContract.AUTHORITY_URI, cv); – Monica Jun 22 '13 at 06:40
  • Does these give you enough information? I don't know whether insert(ContactsContract.AUTHORITY_URI, cv) is correct – Monica Jun 22 '13 at 06:46
  • Try out this [sample project](https://github.com/gwoodhouse/ContactContractSample). this might be helpful – CRUSADER Jun 22 '13 at 06:49
0

just look at your permission:

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

its spelling mistake there: remove s before permission word:

<uses-permission
                android:name="android.permission.WRITE_CONTACTS" />
Sagar Maiyad
  • 12,655
  • 9
  • 63
  • 99