2

This is a small assignment to insert simple text data to sqlite database. I have used SQLiteOpenHelper class for this and used given code. I've found some similiar questions here but not the exact same. Please help me out. Here is the exception-

11-14 21:59:17.786 5746-5763/? E/DatabaseUtils: Writing exception to parcel
                                            java.lang.SecurityException: Neither user 10169 nor current process has android.permission.READ_PROFILE.
                                                at android.app.ContextImpl.enforce(ContextImpl.java:1921)
                                                at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1950)
                                                at android.content.ContextWrapper.enforceCallingOrSelfPermission(ContextWrapper.java:600)
                                                at com.android.providers.contacts.ProfileProvider.enforceReadPermission(ProfileProvider.java:54)
                                                at com.android.providers.contacts.ProfileProvider.query(ProfileProvider.java:84)
                                                at com.android.providers.contacts.ContactsProvider2.query(ContactsProvider2.java:5066)
                                                at android.content.ContentProvider$Transport.query(ContentProvider.java:214)
                                                at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
                                                at android.os.Binder.execTransact(Binder.java:446)

I've used following permissions in my AndroidManifest.XML file:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.Manifest.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

this is my DBHelper class-

public class DBHelper extends SQLiteOpenHelper {

public static final String DATABASE_NAME = "CANDIDATES";
public static final String TABLE_NAME = "CANDIDATES_TABLE";
public static final String NAME = "NAME";
public static final String AGE = "AGE";
public static final String QUAL = "QUALIFICATION";
public static final String SALARY = "SALARY";
public static final String ADD = "ADDRESS";
public static final String DES = "DESCRIPTION";
Context context;


public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
    this.context=context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table "+ TABLE_NAME +" ("+NAME+" TEXT,"+AGE+" INTEGER,"+QUAL+" TEXT,"+SALARY+" INTEGER,"+ADD+" TEXT,"+DES+" TEXT )");

}

public boolean insertData(String name,int age,String qual,int salary, String add, String des){
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues contentValues=new ContentValues();
    contentValues.put(NAME,name);
    contentValues.put(AGE,age);
    contentValues.put(QUAL,qual);
    contentValues.put(SALARY,salary);
    contentValues.put(ADD,add);
    contentValues.put(DES,des);
    long result=db.insert(TABLE_NAME,null,contentValues);
    Log.v("check",name+age+""+qual+salary+""+add+des);
    if(result==-1){
        return false;
    }
    else return true;
}

public Cursor getData(){
    SQLiteDatabase db=this.getWritableDatabase();
    Cursor cursor=db.rawQuery("Select * from"+TABLE_NAME,null);
    return cursor;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);
}

}

Wijay Sharma
  • 439
  • 7
  • 17

3 Answers3

2

You need to ask runtime permissions. Adding manifest is not enough for newer versions.

if (ContextCompat.checkSelfPermission(thisActivity,
            Manifest.permission.READ_CONTACTS)
    != PackageManager.PERMISSION_GRANTED) {

// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
        Manifest.permission.READ_CONTACTS)) {

    // Show an explanation to the user *asynchronously* -- don't block
    // this thread waiting for the user's response! After the user
    // sees the explanation, try again to request the permission.

} else {

    // No explanation needed, we can request the permission.

    ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.READ_CONTACTS},
            MY_PERMISSIONS_REQUEST_READ_CONTACTS);

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
    // app-defined int constant. The callback method gets the
    // result of the request.
}
}
Emre Aktürk
  • 3,306
  • 2
  • 18
  • 30
  • Hi @EmreAktürk, Sorry but I don't need to read contacts in my app. Its just a simple app to add data to the sqlite table. I don't know why app shows this exception on the run time. (Its not even crashing the app) – Wijay Sharma Nov 15 '17 at 05:22
  • Hi @Wijay Sharma, There is a class that named DatabaseUtils that trying to read contacts. You can understand it from the first line of error log. This class should be in a dependency of you are using. If its not crashing, probably it is in try-catch block. – Emre Aktürk Nov 16 '17 at 11:41
-1

You need to add this line to your manifest:

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

enter image description here

Maicon Hellmann
  • 450
  • 3
  • 13
-2

My problem is solved by adding this permission to manifest:

android.permission.WRITE_SETTINGS
Cà phê đen
  • 1,883
  • 2
  • 21
  • 20