Android SecurityException:权限被拒绝
Android SecurityException: Permission Denial
我的 Android 应用程序存在权限问题:
这里是日志。
06-25 17:25:35.862 12039-13799/? E/DatabaseUtils﹕ Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1580, uid=10108 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:453)
06-25 17:25:35.870 1580-2997/? E/iu.UploadsManager﹕ Insufficient permissions to access media store
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1580, uid=10108 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:498)
at android.content.ContentResolver.query(ContentResolver.java:441)
at ifh.i(PG:5904)
at ifh.h(PG:619)
at ifh.<init>(PG:187)
at iel.c(PG:4045)
at gen_binder.root.RootModule$Generated.a(PG:1131)
at nmw.e(PG:415)
at nmw.b(PG:242)
at nmw.a(PG:207)
at nmw.a(PG:493)
at ier.a(PG:2195)
at ier.onPerformSync(PG:125)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:272)
06-25 17:24:37.031 1580-1611/? E/GooglePlusContactsSync﹕ Failed to clear out contacts
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2ForLG from ProcessRecord{9557af9 1580:com.google.android.apps.plus/u0a108} (pid=1580, uid=10108) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3652)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4866)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2020)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1488)
at android.content.ContentResolver.query(ContentResolver.java:482)
at android.content.ContentResolver.query(ContentResolver.java:441)
at dpm.a(PG:397)
at dpm.b(PG:1345)
at dpn.run(PG:282)
at java.lang.Thread.run(Thread.java:818)
我知道这是一个权限问题,我可以像这样授予权限来解决:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
然后使用它来设置对我的提供商的权限:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
问题是我不知道我必须在项目的哪一部分提出此权限请求:
我正在使用 Auth0 Custom UI 登录验证,Mysqlite 作为内部数据库,GoogleCards 作为 ListView。
你能帮我了解如何授予此权限拒绝吗?
非常感谢
自然要放在任意Activity里面。您的应用 运行 是否需要此功能?在应用程序启动时询问。如果您的应用程序可以在没有它的情况下运行 - 在不可避免地阅读联系人时询问(在一个视图中)。
好吧,我建议使用 Ask,它是一个权限帮助程序库。
使用它,您无需编写太多代码,只需两行代码即可为您完成所有工作。
这里是使用详情。
首先,在你的 build.gradle
:
中编译依赖
dependencies {
compile 'com.vistrav:ask:2.4'
}
然后在 OnCreate
中 activity 的开头或按任何按钮,您可以这样请求许可:
Ask.on(this)
.forPermissions(Manifest.permission.READ_CONTACTS)
.go();
就是这样。
您可以找到更多信息here。我几乎在我所有的项目中都使用它,而且简单易行。
Basically you have to check for these permission just before the code where you want to access the contacts.
First of all check for the android version,if it is >=23 then
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else
{
// do here contact related work say you wwant to fetch the name and number from contact,you can do that task here.
}
You have to override this method:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
对于 6.0 以下的 android 版本,只需在清单中定义堆栈跟踪(READ_CONTACTS、WRITE_CONTACTS 等)中提到的权限即可。
对于 android 版本 6.0 及更高版本,您必须请求用户授予权限。理想情况下,它应该在绝对必要时完成,例如当用户导航到您的应用程序中需要该权限的部分时。已解释 here.
我的 Android 应用程序存在权限问题: 这里是日志。
06-25 17:25:35.862 12039-13799/? E/DatabaseUtils﹕ Writing exception to parcel
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1580, uid=10108 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605)
at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480)
at android.content.ContentProvider$Transport.query(ContentProvider.java:211)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:453)
06-25 17:25:35.870 1580-2997/? E/iu.UploadsManager﹕ Insufficient permissions to access media store
java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1580, uid=10108 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
at android.os.Parcel.readException(Parcel.java:1599)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
at android.content.ContentResolver.query(ContentResolver.java:498)
at android.content.ContentResolver.query(ContentResolver.java:441)
at ifh.i(PG:5904)
at ifh.h(PG:619)
at ifh.<init>(PG:187)
at iel.c(PG:4045)
at gen_binder.root.RootModule$Generated.a(PG:1131)
at nmw.e(PG:415)
at nmw.b(PG:242)
at nmw.a(PG:207)
at nmw.a(PG:493)
at ier.a(PG:2195)
at ier.onPerformSync(PG:125)
at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:272)
06-25 17:24:37.031 1580-1611/? E/GooglePlusContactsSync﹕ Failed to clear out contacts
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.contacts.ContactsProvider2ForLG from ProcessRecord{9557af9 1580:com.google.android.apps.plus/u0a108} (pid=1580, uid=10108) requires android.permission.READ_CONTACTS or android.permission.WRITE_CONTACTS
at android.os.Parcel.readException(Parcel.java:1599)
at android.os.Parcel.readException(Parcel.java:1552)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:3652)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:4866)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2020)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1488)
at android.content.ContentResolver.query(ContentResolver.java:482)
at android.content.ContentResolver.query(ContentResolver.java:441)
at dpm.a(PG:397)
at dpm.b(PG:1345)
at dpn.run(PG:282)
at java.lang.Thread.run(Thread.java:818)
我知道这是一个权限问题,我可以像这样授予权限来解决:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
然后使用它来设置对我的提供商的权限:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
问题是我不知道我必须在项目的哪一部分提出此权限请求: 我正在使用 Auth0 Custom UI 登录验证,Mysqlite 作为内部数据库,GoogleCards 作为 ListView。
你能帮我了解如何授予此权限拒绝吗?
非常感谢
自然要放在任意Activity里面。您的应用 运行 是否需要此功能?在应用程序启动时询问。如果您的应用程序可以在没有它的情况下运行 - 在不可避免地阅读联系人时询问(在一个视图中)。
好吧,我建议使用 Ask,它是一个权限帮助程序库。
使用它,您无需编写太多代码,只需两行代码即可为您完成所有工作。
这里是使用详情。
首先,在你的 build.gradle
:
dependencies {
compile 'com.vistrav:ask:2.4'
}
然后在 OnCreate
中 activity 的开头或按任何按钮,您可以这样请求许可:
Ask.on(this)
.forPermissions(Manifest.permission.READ_CONTACTS)
.go();
就是这样。
您可以找到更多信息here。我几乎在我所有的项目中都使用它,而且简单易行。
Basically you have to check for these permission just before the code where you want to access the contacts.
First of all check for the android version,if it is >=23 then
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
else
{
// do here contact related work say you wwant to fetch the name and number from contact,you can do that task here.
}
You have to override this method:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
对于 6.0 以下的 android 版本,只需在清单中定义堆栈跟踪(READ_CONTACTS、WRITE_CONTACTS 等)中提到的权限即可。
对于 android 版本 6.0 及更高版本,您必须请求用户授予权限。理想情况下,它应该在绝对必要时完成,例如当用户导航到您的应用程序中需要该权限的部分时。已解释 here.