Monday, 25 June 2018

Caused by: java.lang.SecurityException when use content resolver in android

I am reading android messages, calendars data etc. using content resolver. i added permission into manifest file. and also implemented runtime permission for reading/writing SMS & calendar. still i am getting below issue on some devices which are running in android 7.0 or higher.
Caused by: java.lang.SecurityException: 
  at android.os.Parcel.readException (Parcel.java:1693)
  at android.os.Parcel.readException (Parcel.java:1646)
  at android.app.ActivityManagerProxy.getContentProvider (ActivityManagerNative.java:4912)
  at android.app.ActivityThread.acquireProvider (ActivityThread.java:6043)
  at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider
 (ContextImpl.java:2474)
  at android.content.ContentResolver.acquireUnstableProvider (ContentResolver.java:1521)
  at android.content.ContentResolver.query (ContentResolver.java:520)
  at android.content.ContentResolver.query (ContentResolver.java:478)
  at com.allbackup.ui.activity.MsgsActivity.c (MsgsActivity.java:301)
  at com.allbackup.ui.activity.MsgsActivity$c.a (MsgsActivity.java:201)
  at com.allbackup.ui.activity.MsgsActivity$c.doInBackground (MsgsActivity.java:186)

above is the stacktrace of error while reading SMS. and below is the code for that:
Uri message = Uri.parse("content://sms/");
            ContentResolver cr = getContentResolver();

            Cursor c = cr.query(message, null, null, null, null);
            //startManagingCursor(c);
            int totalSMS = c.getCount();

            if (c.moveToFirst()) {
if(c.getString(c.getColumnIndex("address")) != null){
                        add = c.getString(c.getColumnIndexOrThrow("address")).
replaceAll("[\\s\\-()]", "");
                        map.put("address", c.getString(c.getColumnIndexOrThrow("address")).
replaceAll("[\\s\\-()]", ""));
                    }else{

                        map.put("address", "");
                    }
if(!add.isEmpty()){
                        Uri lookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,
 Uri.encode(add));
                        Cursor cno = getContentResolver().query(lookupUri, new String[]{
ContactsContract.
Data.DISPLAY_NAME_PRIMARY, ContactsContract.Data.PHOTO_THUMBNAIL_URI},null,null,null);
                        if(cno.getCount()>0){
                            try {
                                cno.moveToFirst();
                                map.put("name", cno.getString(0));
                                map.put("photo", cno.getString(1));
                            } catch (Exception e) {
                                // TODO: handle exception
                            }finally{
//                              cno.close();
                            }
                        }else{
                            map.put("name", "");
                            map.put("photo", "");

                        }
                        if(cno!=null)
                            cno.close();
                    }
}

I am getting error on below line:
Cursor cno = getContentResolver().query(lookupUri, new
 String[]{ContactsContract.Data.DISPLAY_NAME_PRIMARY, 
ContactsContract.Data.PHOTO_THUMBNAIL_URI},null,null,null);



from Caused by: java.lang.SecurityException when use content resolver in android

No comments:

Post a Comment