【问题标题】:storage permissions not being asked未询问存储权限
【发布时间】:2019-05-07 19:45:42
【问题描述】:

我已将这些权限添加到清单文件中

<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" />

但是当用户安装应用程序时,他被要求并允许的唯一权限是位置

我也想向用户请求存储权限。这怎么可能?

【问题讨论】:

标签: android permissions storage


【解决方案1】:

使用运行时权限代码在内部存储中添加数据。您可以为此使用 dexture 或简单权限库。

【讨论】:

    【解决方案2】:

    您是否尝试过 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
    }
    

    https://developer.android.com/training/permissions/requesting#java

    【讨论】:

      【解决方案3】:

      对于“危险”权限(包括存储、位置、相机、日历等),您需要在安装时而不是在需要时向用户请求权限。 请务必在每次需要权限时进行检查,因为用户可以随时撤销权限。

      检查权限(在 Kotlin 中)(在本例中为 FINE_LOCATION,将其更改为您需要请求的任何权限):

              if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
              != PackageManager.PERMISSION_GRANTED
          ) {
      //if permission not granted, start function to request permission
      //MY_PERMISSIONS_REQUEST is the requestCode, an int > 0
              ActivityCompat.requestPermissions(
                  this,
                  arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                  MY_PERMISSIONS_REQUEST
              )
          }
      

      然后再重写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.
              }
          }
      
      }
      

      您也可以一次请求多个权限。 Android 6.0 multiple permissions

      【讨论】:

        猜你喜欢
        • 2013-05-21
        • 2018-11-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-07
        • 1970-01-01
        • 2017-07-05
        • 2023-03-08
        相关资源
        最近更新 更多