android Content provider 使用时出现的错误。

2013-07-23 10:04:06
错误提示信息:
java.lang.SecurityException: 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)



test代码:
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;
}

}


情况如下:
开始我已经在模拟器上启动了数据库代码。
但是过了很久后再使用测试代码,这个时候,系统提示错误参数的信息。
于是我再重新启动了下数据库的程序。
再次运行Test程序,就提示这个错误。
...全文
790 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
2013-07-23
  • 打赏
  • 举报
回复
当我对PersonProvier中的MATCHER的匹配URI进行修改的时候,则会如提示如下错误:跟1L一样。 java.lang.SecurityException: Permission Denial: opening provider com.example.db.PersonProvider from ProcessRecord{2c5e1e60 30071:com.dzr.other2/u0a10051} (pid=30071, 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)
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{//在perosnprovider中间加了一个s
		MATCHER.addURI("cn.dzr.personsprovider", "person", PERSONS);
	}
	public boolean onCreate()
	{
		dbOpenHelper = new DbOpenHelper(this.getContext());
		return true;
	}
2013-07-23
  • 打赏
  • 举报
回复
public class AccessContentProvierTest extends AndroidTestCase
{

	public void testInsert()throws Exception
	{
                 //我在此处修改成错误参数后,系统则会提示如下错误。
                    
		Uri uri = Uri.parse("content://cn.dzzr.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);
	}
}
错误提示信息: java.lang.IllegalArgumentException: Unknown URL content://cn.dzzr.personprovider/person at android.content.ContentResolver.insert(ContentResolver.java:862) 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)
qinjuning 2013-07-23
  • 打赏
  • 举报
回复
恩,如果一个<provider >没有设置<intentfilter>默认 exported为false。 度娘吧,很详细,以及有解决方案。
yoxibaga 2013-07-23
  • 打赏
  • 举报
回复
引用 4 楼 fortheother 的回复:
[quote=引用 3 楼 yoxibaga 的回复:] 目测是权限问题。 你检查下manifest里provider节点的exported属性
这个应该如何设置呢。exproted应该设置为多少???[/quote]

android:exported
Whether the content provider is available for other applications to use:
true: The provider is available to other applications. Any application can use the provider's content URI to access it, subject to the permissions specified for the provider.
false: The provider is not available to other applications. Set android:exported="false" to limit access to the provider to your applications. Only applications that have the same user ID (UID) as the provider will have access to it.
The default value is "true" for applications that set either android:minSdkVersion or android:targetSdkVersion to "16" or lower. For applications that set either of these attributes to "17" or higher, the default is "false".

You can set android:exported="false" and still limit access to your provider by setting permissions with the permission attribute.
android文档里的内容。若你想暴露provider给其他应用程序,设置为true.反之为false。大体是这个意思,
2013-07-23
  • 打赏
  • 举报
回复
引用 3 楼 yoxibaga 的回复:
目测是权限问题。 你检查下manifest里provider节点的exported属性
这个应该如何设置呢。exproted应该设置为多少???
yoxibaga 2013-07-23
  • 打赏
  • 举报
回复
目测是权限问题。 你检查下manifest里provider节点的exported属性

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧