App crashes when trying to set song as specific friend in android studio, app crashes at getContentresolver

77 views Asked by At

I am trying to make ringtone app. I am new to android developement and by doing this I am also learning a lot. I managed to set songs as ringtone for all contacts,However when I try to set song for specific friend my app crashes. Firstly I am using this function to read contacts when I click on "set as Specific friend" button:

public void loadPhoneBook (){
        Intent intent = new Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
        this.startActivityForResult(intent,PICK_CONTACT);

} 

After that I try with this function :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == PICK_CONTACT)
        {
            Uri contactData =data.getData();
            Cursor c = getContentResolver().query(contactData,null,null,null,null);
            if(c.moveToFirst())
            {
                String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                Toast.makeText(this,name,Toast.LENGTH_SHORT).show();
            }
            String pesma = "";
            Uri uri = Uri.parse("android.resource://"+getBaseContext().getPackageName()+"/raw/"+nizModela.get(customAdapter.lastSetAsPosition).getNaziv());
            File f = customAdapter.SaveFileOnInternalMemory(uri,customAdapter.lastSetAsPosition,getContentResolver());
            Uri uri1 = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());

            final ContentValues cv = new ContentValues(1);
            cv.put(ContactsContract.Contacts.CUSTOM_RINGTONE,f.getAbsolutePath());




                getContentResolver().update(contactData,cv,null,null);
            }

My code crashes at this line ----->"getContentResolver().update(contactData,cv,null,null);"

Here is the Fatal Exception message :

FATAL EXCEPTION: main

Process: com.example.petar.fejsbuk, PID: 30592
                                                                       java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/667r1279-293F314353452F392D5B29474B2935.2597r2596-293F314353452F392D5B29474B2935/1296 flg=0x1 }} to activity {com.example.petar.fejsbuk/com.example.petar.fejsbuk.MainActivity}: java.lang.SecurityException: Permission Denial: writing com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/667r1279-293F314353452F392D5B29474B2935.2597r2596-293F314353452F392D5B29474B2935/1296 from pid=30592, uid=10188 requires android.permission.WRITE_CONTACTS, or grantUriPermission()
                                                                           at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                           at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                           at android.app.ActivityThread.access$1400(ActivityThread.java:150)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                        Caused by: java.lang.SecurityException: Permission Denial: writing com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/contacts/lookup/667r1279-293F314353452F392D5B29474B2935.2597r2596-293F314353452F392D5B29474B2935/1296 from pid=30592, uid=10188 requires android.permission.WRITE_CONTACTS, or grantUriPermission()
                                                                           at android.os.Parcel.readException(Parcel.java:1620)
                                                                           at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                           at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                           at android.content.ContentProviderProxy.update(ContentProviderNative.java:568)
                                                                           at android.content.ContentResolver.update(ContentResolver.java:1365)
                                                                           at com.example.petar.fejsbuk.MainActivity.onActivityResult(MainActivity.java:320)
                                                                           at android.app.Activity.dispatchActivityResult(Activity.java:6499)
                                                                           at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                           at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                           at android.app.ActivityThread.access$1400(ActivityThread.java:150) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

As far as manifest is concerned I have these permissions :

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

I do not know what grantUriPermission () is for or how to use it, please help!!

0

There are 0 answers