1

I have two apps from which i want to share data between the apps using content provider.What i do, i have create Content Provider in app A. Below is Manifest.xml file of app A.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.kil.demoexample">
    <uses-permission android:name="android.permission.INTERNET"/>
    <permission android:name="com.kil.demoexample.ContentProvider.StudentsProvider.READ_DATABASE" android:protectionLevel="normal"/>
    <permission android:name="com.kil.demoexample.ContentProvider.StudentsProvider.WRITE_DATABASE" android:protectionLevel="normal"/>
    <application
        android:debuggable="true"
        android:name=".myApplication"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="HardcodedDebugMode">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Login.LoginActivity"/>
        <activity android:name=".Login.ObjectPassActivity"/>
        <activity android:name=".Login.SignUpActivity"/>
        <provider
            android:name="com.kil.demoexample.ContentProvider.StudentsProvider"
            android:authorities="com.kil.demoexample.ContentProvider.StudentsProvider"
            android:exported="true"
            android:readPermission="com.kil.demoexample.ContentProvider.StudentsProvider.READ_DATABASE"
            android:writePermission="com.kil.demoexample.ContentProvider.StudentsProvider.WRITE_DATABASE"/>
    </application>

</manifest>

Now in App B I am trying to access content provider on App A. Below in Manifest.xml file of app B.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kil.practicepro">
    <uses-permission android:name="android.permission.INTERNET"/>

    <uses-permission android:name="com.kil.demoexample.ContentProvider.StudentsProvider.WRITE_DATABASE"/>
    <uses-permission android:name="com.kil.demoexample.ContentProvider.StudentsProvider.READ_DATABASE"/>
    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".Activity.LoginActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".MainActivity"/>
    </application>

</manifest>

Now from app B i am trying below code to get data from app A content provider

 ContentResolver cr=getContentResolver();
        String URL = "content://com.kil.demoexample.ContentProvider.StudentsProvider/students";

        Uri uri = Uri.parse(URL);
        Cursor c=cr.query(uri,null,null,null,null);
          if (c != null) {
            if (c.moveToFirst()) {
                do {
                    Log.d("column1",c.getString(0));
                } while (c.moveToNext());
                c.close();
            }
        }else{
            Toast.makeText(this, "CursorIsNull", Toast.LENGTH_SHORT).show();

        }

When iam trying to get data within app A it works fine but When i am trying to access content provider from app B it through me an error. I have check almost all the post on Stackoverflow. According to all the post i have set my permission in both AndroidManifest.xml but it still giving me an error of **java.lang.SecurityException: Permission Denial: opening provider ** is there is any mistake in my files please help me out from it.

amit semwal
  • 345
  • 3
  • 16
  • Firstly, the permissions don't match in App A's manifest: `com.kil.demoexample.ContentProvider.StudentsProvider.READ_DATABASE` versus `com.kil.demoexample.READ_DATABASE`. Secondly, permissions you're defining go in `` elements, not ``. Also, make sure that App A is installed first. You'll need to uninstall/reinstall both after you make these changes. – Mike M. Mar 21 '18 at 05:55
  • I have make permission same in both file but still giving me an error @MikeM. – amit semwal Mar 21 '18 at 06:03
  • read [Define a Custom App Permission](https://developer.android.com/guide/topics/permissions/defining.html) – pskink Mar 21 '18 at 06:05
  • The permissions on the `` don't match. And you still have `` elements for them in App A's manifest. – Mike M. Mar 21 '18 at 06:06
  • thanks for helping, but I just change the permission on but still not working @MikeM. – amit semwal Mar 21 '18 at 06:17
  • I'm sorry, but I'm not going to repeat myself again. Please read over my previous comments again very carefully, and consult the link that pskink provided. – Mike M. Mar 21 '18 at 06:18
  • 1
    thank you so much @MikeM. and pskink for the help. I have go through the "Custome app permission" and It is working fine now :) thanks for saving my time guys. – amit semwal Mar 21 '18 at 07:08

0 Answers0