ContentProvier使用时:java.lang.SecurityException: Permission Denial:XXX解决办法

时间:2022-10-11 23:24:15
提示错误
 
Permission Denial:opening provider com.example.db.PersonProvider from ProcessRecord{2c6e34f0 24384:com.dzr.other2/u0a10051} (pid=24384, uid=10051) that is not exported from uid 10050
at android.os.Parcel.readException(Parcel.java:1425)
at android.os.Parcel.readException(Parcel.java:1379)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:2530)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4460)
at android.app.ContextImpl$ApplicationContentResolver.acquireProvider(ContextImpl.java:1987)
at android.content.ContentResolver.acquireProvider(ContentResolver.java:1054)
at android.content.ContentResolver.insert(ContentResolver.java:860)
at com.dzr.test.AccessContentProvierTest.testInsert(AccessContentProvierTest.java:21)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:190)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:175)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:555)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1661)</span>


 
测试代码
</pre><pre code_snippet_id="468846" snippet_file_name="blog_20140915_5_275383" name="code" class="java">
package com.dzr.test;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.test.AndroidTestCase;

public class AccessContentProvierTest extends AndroidTestCase
{

	public void testInsert()throws Exception
	{
		Uri uri = Uri.parse("content://cn.dzr.personprovider/person");
		ContentResolver resolver = this.getContext().getContentResolver();
		ContentValues values = new ContentValues();
		
		values.put("name", "laoli");
		values.put("phone", "13786788939");
		values.put("amount", 12212121);
		
		resolver.insert(uri, values);
	}
}

程序代码

package com.example.db;

import com.example.service.DbOpenHelper;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class PersonProvider extends ContentProvider
{

	private DbOpenHelper dbOpenHelper;
	private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
	private static final int PERSONS = 1;
	static{
		MATCHER.addURI("cn.dzr.personprovider", "person", PERSONS);
	}
	public boolean onCreate()
	{
		dbOpenHelper = new DbOpenHelper(this.getContext());
		return true;
	}
	@Override
	public int delete(Uri arg0, String arg1, String[] arg2)
	{
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri)
	{
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values)
	{
		SQLiteDatabase db = this.dbOpenHelper.getWritableDatabase();
		
		switch(MATCHER.match(uri))
		{
		case 1:
			long rowid = db.insert("person", null, values);
		
		//	Uri insertUri = Uri.parse("content://cn.dzr.personprovider/person" + rowid);
			Uri insertUri = ContentUris.withAppendedId(uri, rowid);
			return insertUri;
			
		default:
			throw new IllegalArgumentException("this is an unknow Uri:" + uri);
			
			  
		}		
	}

	

	@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;
	}

}
解决办法

AndroidManifest.xml里加入

 <provider   android:exported = "true" />