Android 未请求存储权限
我已将这些权限添加到清单文件中Android 未请求存储权限,android,permissions,storage,Android,Permissions,Storage,我已将这些权限添加到清单文件中 <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.INTERNET" /> &
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
但是,当用户安装应用程序时,他唯一被请求和允许的权限是位置
我还想向用户请求存储权限。这怎么可能呢?在将数据添加到内部存储器的地方使用运行时权限代码。您可以使用dexture或easy权限库进行此操作。您是否尝试过Android文档中指出的内容:
// Here, thisActivity is the current activity
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Permission is not granted
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an explanation 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; 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 {
// Permission has already been granted
}
对于“危险”权限(包括存储、位置、照相机、日历等),您需要请求用户的权限,而不是在安装时,而是在需要时。
每次需要权限时都要检查,这一点很重要,因为用户可以随时撤销权限
要检查权限(在Kotlin中)(在本例中,它位于FINE_位置,请将其更改为您需要请求的任何权限):
然后重写onRequestPermissionsResult函数:
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
MY_PERMISSIONS_REQUEST -> {
// If request is cancelled, the result arrays are empty.
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
//granted - you are not able to use Location Functionality
} else {
// permission denied
Toast.makeText(this, "Location Functionality Disabled.", Toast.LENGTH_LONG).show()
}
return
}
// Add other 'when' lines to check for other
// permissions this app might request.
else -> {
// Ignore all other requests.
}
}
}
覆盖onRequestPermissionsResult(
请求代码:Int,
权限:数组,GrantResult:IntArray
) {
何时(请求代码){
我的权限请求->{
//如果取消请求,则结果数组为空。
if((grantResults.isNotEmpty()&&grantResults[0]==PackageManager.PERMISSION_已授予)){
//已授予-您无法使用位置功能
}否则{
//拒绝许可
Toast.makeText(此“位置功能已禁用”,Toast.LENGTH\u LONG.show()
}
返回
}
//添加其他“何时”行以检查其他
//此应用可能请求的权限。
其他->{
//忽略所有其他请求。
}
}
}
您还可以一次请求多个权限。
override fun onRequestPermissionsResult(
requestCode: Int,
permissions: Array<String>, grantResults: IntArray
) {
when (requestCode) {
MY_PERMISSIONS_REQUEST -> {
// If request is cancelled, the result arrays are empty.
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
//granted - you are not able to use Location Functionality
} else {
// permission denied
Toast.makeText(this, "Location Functionality Disabled.", Toast.LENGTH_LONG).show()
}
return
}
// Add other 'when' lines to check for other
// permissions this app might request.
else -> {
// Ignore all other requests.
}
}
}