Search code examples
androidandroid-securityandroid-securityexception

Permission denial while accessing content provider of another application


Tutorial I am using for understanding Content Providers

AppA: https://www.tutorialspoint.com/android/android_content_providers.htm

I have created another application which I intend to use, to access the Content Provider of the tutorial application

AppB Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.MyApplication2">
    <uses-permission android:name="com.example.MyApplication.StudentProvider.READ_PERMISSION"/>
    <uses-permission android:name="com.example.MyApplication.StudentProvider.WRITE_PERMISSION"/>
    <uses-permission android:name="android.permission.READ_USER_DICTIONARY"/>
    <uses-permission android:name="android.permission.permRead"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

AppB Partial Code:

String URL = "content://com.example.MyApplication.StudentsProvider/students";

Uri uri = Uri.parse(URL);

Cursor c = getContentResolver().query(uri,null,null,null,null);


String _ID = "_id";
String NAME = "name";
String GRADE = "grade";

if (c.moveToFirst()) {
    do{
        Toast.makeText(this,
                c.getString(c.getColumnIndex(_ID)) +
                        ", " +  c.getString(c.getColumnIndex(NAME)) +
                        ", " + c.getString(c.getColumnIndex(GRADE)),
                Toast.LENGTH_SHORT).show();
    } while (c.moveToNext());
}

I am getting the following error:

Caused by: java.lang.SecurityException: Permission Denial: opening provider

Any ideas as to why I am getting this error? Any help is appreciated!

EDIT: I got the Solution! (//This line is new & //Line modified)

String URL = "content://com.example.MyApplication.StudentsProvider/students";
Uri uri = Uri.parse(URL);
ContentResolver cr = getContentResolver(); //This line is new
Cursor c = cr.query(uri,null,null,null,null); //Line modified

String _ID = "_id";
String NAME = "name";
String GRADE = "grade";

if (c.moveToFirst()) {
do{
    Toast.makeText(this,
            c.getString(c.getColumnIndex(_ID)) +
                    ", " +  c.getString(c.getColumnIndex(NAME)) +
                    ", " + c.getString(c.getColumnIndex(GRADE)),
            Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}

Now my AppB is able to read the ContentProvider from AppA, however I still do not understand why this should make a difference!

EDIT2: Already replaced the tutorial Manifest with ChangedManifest XML


Solution

  • Solution: Need to use a Content Resolver to access other application Content Providers after all necessary permissions are declared/requested/granted.