在小部件中,我使用remoteView.setImageViewUri()
显示来自SDCard的图像。此策略可以正常工作,但使用MarshMallow时除外:
错误是:
Unable to open content: file:///storage/emulated/0/Android/data/applicaitonPackage/files/file.png
open failed: EACCESS (Permission denied)
很明显,这是一个权限问题,但我不知道如何为小部件容器提供权限,理论上(请参阅注释1)图像已经存储在共享存储中。
注意1:存储图像的目录是Environment.getExternalStorageDirectory()
下的共享存储
注意2:应用程序不适用于MarshMallow,而是使用targetSdkVersion=15
注意3:不要只回复解释MarshMallow中新的运行时权限。我已经知道权限的变化,这不是问题,因为应用程序是针对SDKVersion 15的,并且应用程序访问外部存储没有任何问题,问题是小部件容器,我怀疑它没有权限。
发布于 2016-03-29 14:29:13
嗨,这里是android M的设置权限的几个步骤,记住你也应该在清单文件中声明相同的权限。
步骤1.声明全局变量:
public static final int MY_PERMISSIONS_REQUEST_LOCATION = 0;
步骤2.在您的主活动中使用此代码
private void locationpermission() {
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(activity
,
Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
Manifest.permission.ACCESS_COARSE_LOCATION)) {
// 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(activity,
new String[]{Manifest.permission.ACCESS_COARSE_LOCATION},
MY_PERMISSIONS_REQUEST_LOCATION);
// 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_LOCATION: {
// 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
}
}
步骤3.在oncreate方法中调用此方法,
locationpermission();以及你可以从这里调用的任何权限,以及你可以在override method onRequestPermissionsResult中得到的每一个结果。
谢谢你
希望这能对你有所帮助(Y)。
请参考此处的示例
https://www.dropbox.com/s/w29sljy0zpwwm61/MyApplication.zip?dl=0
发布于 2016-04-04 22:24:02
@Docs说
在运行时请求权限
从Android 6.0 (API级别23)开始,用户在应用程序运行时授予应用程序权限,而不是在安装应用程序时授予权限。这种方法简化了应用程序的安装过程,因为用户在安装或更新应用程序时不需要授予权限。它还让用户对应用程序的功能有了更多的控制;
因此,这是有意义的,尽管您在清单中声明了权限,但仍然得到了拒绝的权限
我建议您阅读如何获得运行时权限
http://developer.android.com/training/permissions/requesting.html
以下是Docs提供的示例
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_WRITE_EXTERNAL_STORAGE is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
根据您的要求更改权限
希望它能指引你找到正确的方向
发布于 2016-04-01 17:02:25
您的清单文件中有此权限吗?
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
https://stackoverflow.com/questions/36127733
复制相似问题