Error trying to get the contact number of the calendar

1

I am making an application to send messages in the first two fields I want to show the contact's name and contact number

the name I have already been able to capture, but when processing the number it shows me the following error, I have already assigned the permissions in the manifest.

This is the code you use to get the contact data

 private void renderContact(Uri uri){

    EditText edtNombre = (EditText)findViewById(R.id.edtNombre);
    EditText edtNumTel = (EditText)findViewById(R.id.edtNumTel);

    edtNombre.setText(getName(uri));
    edtNumTel.setText(getPhone(uri));
}

private String getName(Uri uri){
    String name = null;

    ContentResolver contentResolver = getContentResolver();

    Cursor c = contentResolver.query(
            uri,
            new String[]{ContactsContract.Contacts.DISPLAY_NAME},
            null,
            null,
            null);
    if (c.moveToFirst()){
        name = c.getString(0);
    }

    c.close();

    return name;
}

private String getPhone(Uri uri){
    String id = null;
    String phone = null;

    Cursor contacCursor = getContentResolver().query(
            uri,
            new String[]{ContactsContract.Contacts._ID},
            null,
            null,
            null);
    if (contacCursor.moveToFirst()){
        id = contacCursor.getString(0);
    }
    contacCursor.close();

    String selectionArgs = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
            ContactsContract.CommonDataKinds.Phone.TYPE+"= " +
            ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE;

    Cursor phoneCursor = getContentResolver().query(
            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
            new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
            selectionArgs,
            new String[]{id},
            null);
    if (phoneCursor.moveToFirst()){
        phone = phoneCursor.getString(0);
    }
    phoneCursor.close();

    return phone;
}

ERROR!

07-07 12:00:40.211 3891-3891/com.alexiscaballero.appdemo E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: com.alexiscaballero.appdemo, PID: 3891
                                                                       java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/2522i6ea09ce78ba13648.3789r131-292B393529393F4335/131 flg=0x1 }} to activity {com.alexiscaballero.appdemo/com.alexiscaballero.appdemo.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=3891, uid=10105 requires android.permission.READ_CONTACTS, or grantUriPermission()
                                                                           at android.app.ActivityThread.deliverResults(ActivityThread.java:4120)
                                                                           at android.app.ActivityThread.handleSendResult(ActivityThread.java:4163)
                                                                           at android.app.ActivityThread.-wrap20(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1548)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:154)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6208)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)
                                                                        Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.contacts.ContactsProvider2 uri content://com.android.contacts/data/phones from pid=3891, uid=10105 requires android.permission.READ_CONTACTS, or grantUriPermission()
                                                                           at android.os.Parcel.readException(Parcel.java:1684)
                                                                           at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                           at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                           at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                                                                           at android.content.ContentResolver.query(ContentResolver.java:534)
                                                                           at android.content.ContentResolver.query(ContentResolver.java:475)
                                                                           at com.alexiscaballero.appdemo.MainActivity.getPhone(MainActivity.java:178)
                                                                           at com.alexiscaballero.appdemo.MainActivity.renderContact(MainActivity.java:136)
                                                                           at com.alexiscaballero.appdemo.MainActivity.onActivityResult(MainActivity.java:125)
                                                                           at android.app.Activity.dispatchActivityResult(Activity.java:6937)
                                                                           at android.app.ActivityThread.deliverResults(ActivityThread.java:4116)
                                                                           at android.app.ActivityThread.handleSendResult(ActivityThread.java:4163) 
                                                                           at android.app.ActivityThread.-wrap20(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1548) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:154) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6208) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:781)  
    
asked by Alexis Caballero 07.07.2017 в 17:41
source

1 answer

2

The problem you have is that Android 6.0 changed the permissions and now besides adding the permissions in the manifest you have to ask at run time if the permissions are granted (With all except the Internet this happens).

To ask if you have contact permission you should do this:

        if (ContextCompat.checkSelfPermission(getContext(),
                Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            //SOLICITAR PERMISO
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, 123);
        }else{
            //CONCEDIDO
            renderContact(uri);
        }

and about writing this method to take the result:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                       @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    switch (requestCode){
        case 123:
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
                renderContact(uri);
            }else{
                //NEGADO
            }
            break;
    }
}

Note that the second parameter of the requestPermissions is an id so you can identify the request, usually it becomes a constant to avoid bugs.

I hope I helped you.

    
answered by 08.07.2017 / 01:34
source