【问题标题】:Permission denial: opening provider许可拒绝:开放提供者
【发布时间】:2013-01-16 22:14:37
【问题描述】:

我创建了一个自定义内容提供程序,将由更多应用程序访问。我在提供程序 AndroidManifest.xml 文件中包含了权限标签,在第二个应用程序中,我包含了使用权限标签,但没有成功。 Logcat 给我看:

java.lang.SecurityException: Permission Denial: opening provider com.company.contentprovider.AplicacaoContentProvider requires READ_DATABASE or WRITE_DATABASE.

我搜索过类似的问题,但似乎一切都是正确的。有任何想法吗 ? 谢谢 !!!

这是我的提供者 AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.contentprovider"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />
<permission android:name="READ_DATABASE" android:label="@string/app_read"       android:protectionLevel="normal"></permission>
<permission android:name="WRITE_DATABASE" android:label="@string/app_write" android:protectionLevel="normal"></permission>

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".CompanyProvider"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider android:name="AplicacaoContentProvider"
        android:authorities="com.company.contentprovider"
        android:exported="true"
        android:readPermission="@string/app_read"
        android:writePermission="@string/app_write"
       />
</application>

这是我的第二个应用程序 AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testeprovider"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="16" />
<uses-permission android:name="android.permissions.READ_DATABASE"/>
<uses-permission android:name="android.permissioms.WRITE_DATABASE"/>


<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.testeprovider.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

</application>

【问题讨论】:

    标签: android


    【解决方案1】:

    上面的答案让我有点困惑。但我现在明白了。我也想发布我的解决方案。也许对某人来说更容易理解。

    第一个应用程序 A 是具有 SQLite 数据库和“自定义内容提供程序”的应用程序。应用 B 通过 ContentResolver 使用来自应用 A 的数据库。

    这是来自 App A 的 AndroidManifest.xml 文件:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.test"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk android:minSdkVersion="10" />
    
    
    <permission android:name="de.test.READ_DATABASE" android:protectionLevel="normal" />
    <permission android:name="de.test.WRITE_DATABASE" android:protectionLevel="normal" />
    
    <application
        android:debuggable="true"
        ... >
        ...
        ...
        <provider
            android:name="de.test.TestContentProvider"
            android:authorities="de.test.ContentProvider"
            android:exported="true"
            android:readPermission="de.test.READ_DATABASE"
            android:writePermission="de.test.WRITE_DATABASE" />
        ...
        ...
    </application>
    

    好的,这是来自 App B 的 AndroidManifest.xml 文件。重要的是带有“使用权限”的部分:

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.test.testercontentprovider"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="17" />
    
    <uses-permission android:name="de.test.READ_DATABASE" />
    <uses-permission android:name="de.test.WRITE_DATABASE" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="de.test.testercontentprovider.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

    App A 的 ContentProvider 的代码如下所示:

    public class TestContentProvider extends ContentProvider {
    
    public static final String AUTHORITY = "de.test.TestContentProvider";
    
    public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY
            + "/" + "nameoftable");
    
    
    @Override
    public boolean onCreate() {
        ...
        return true;
    }
    
    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        // TODO Auto-generated method stub
                return null;
    }
    
    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }
    
    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        // TODO Auto-generated method stub
        return 0;
    }
    
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        // TODO Auto-generated method stub
        return null;
    }
    
    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }
    }
    

    以及来自 App B 的 ContentResolver 的代码:

    public class MainActivity extends Activity {
    
    private static final String TAG = MainActivity.class.getSimpleName();
    public static final String AUTHORITY = "de.test.TestContentProvider";
    public static final String TABLE_NAME = "nameoftable";
    
        ...
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        ContentResolver cr = getContentResolver();
    
        // show entries of db
        listEntries(cr);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private void listEntries(ContentResolver cr) {
        Uri uri = Uri.parse("content://" + AUTHORITY + "/" + TABLE_NAME);
        Cursor c = cr.query(uri, null, null, null, null);
    
        if (c == null) {
            Log.d(TAG, "Cursor c == null.");
            return;
        }
        while (c.moveToNext()) {
            String column1 = c.getString(0);
            String column2 = c.getString(1);
            String column3 = c.getString(2);
    
            Log.d(TAG, "column1=" + column1 + " column2=" + column2 + " column3=" + column3);
        }
        c.close();
    }
    }
    

    我希望这可以帮助人们更好地理解它。

    【讨论】:

      【解决方案2】:

      但似乎一切都是正确的

      不完全是。

      <permission android:name="READ_DATABASE" android:label="@string/app_read"       android:protectionLevel="normal"></permission>
      <permission android:name="WRITE_DATABASE" android:label="@string/app_write" android:protectionLevel="normal"></permission>
      

      首先,你真的真的真的真的真的真的应该在这些权限名称上放置一个命名空间。将它们设为com.company.contentprovider.READ_DATABASEcom.company.contentprovider.WRITE_DATABASE

      <provider android:name="AplicacaoContentProvider"
          android:authorities="com.company.contentprovider"
          android:exported="true"
          android:readPermission="@string/app_read"
          android:writePermission="@string/app_write"
         />
      

      其次,您的android:readPermissionandroid:writePermission 值需要使用来自&lt;permission&gt;android:name 值,而不是android:labelandroid:label 只是一个显示名称。所以,上面的sn-p应该是:

      <provider android:name="AplicacaoContentProvider"
          android:authorities="com.company.contentprovider"
          android:exported="true"
          android:readPermission="com.company.contentprovider.READ_DATABASE"
          android:writePermission="com.company.contentprovider.WRITE_DATABASE"
         />
      

      (不过,明确输入android:exported="true" 可以加分,这是个好主意)

      <uses-permission android:name="android.permissions.READ_DATABASE"/>
      <uses-permission android:name="android.permissioms.WRITE_DATABASE"/>
      

      第三,你的其他清单没有使用你的旧android:name,也不是我建议的修改android:name,也不是android:label,而是完全不同的东西,你选择说这些在android.permission命名空间中,他们不是。这应该是:

      <uses-permission android:name="com.company.contentprovider.READ_DATABASE"/>
      <uses-permission android:name="com.company.contentprovider.WRITE_DATABASE"/>
      

      (虽然com.company.contentprovider.WRITE_DATABASE 可能就足够了——我不知道android:writePermission 是否会自动暗示android:readPermission

      做出这些改变,我想你会有更好的运气。

      【讨论】:

      • 谢谢!明天一上班,我就做这些修改,然后告诉你结果!
      • @MauricioAlencar:“我想在每个应用程序的 AndroidManifest.xml 文件中包含权限和使用权限标签。它会工作吗?” ——那部分很好。事实上,在“插件”类型的情况下,这通常是一种很好的做法。
      • 我有一个奇怪的问题。如果我在内容提供程序应用程序之前安装接收器应用程序,我会得到 java.lang.SecurityException: Permission Denial: opening provider。如果我以其他方式进行操作,效果会很好。
      • android:exported="true" 是解决方案。
      • @Cody: true 已经有一段时间不是 android:exported 的默认值了。对于 android:minSdkVersionandroid:targetSdkVersion 设置为 16 或更低的应用程序,默认情况下会导出提供程序。对于所有其他应用程序,默认情况下不导出提供程序。 IOW,一团糟。
      【解决方案3】:

      我遇到了同样的问题,然后我记得我忘记了

      在 Manifest 中为提供者添加导出标签

         <provider
              android:name=".UsersProvider"
              android:authorities="com.xyz.demoapplication"
              android:exported="true">
         </provider>
      

      要在另一个应用程序中访问,您需要导出 true 这允许其他应用程序从该主机应用程序访问 Content Provider

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题。确保您的权威声明内部 AndroidManifest 和 URL(in URI.parse(URL)) 是相同的。这可能是问题所在。

        【讨论】:

          猜你喜欢
          • 2017-06-04
          • 1970-01-01
          • 2014-08-19
          • 2018-10-28
          • 2015-11-17
          • 1970-01-01
          • 1970-01-01
          • 2018-09-04
          相关资源
          最近更新 更多