【问题标题】:Android 6 runtime permission for accessing gallery访问图库的 Android 6 运行时权限
【发布时间】:2016-02-15 12:54:19
【问题描述】:

我收到了有关 Android 6 (Marshmallow) 运行时权限的问题。如果用户想从图库中挑选照片,我们是否应该请求READ_EXTERNAL_STORAGE 许可?

即使我关闭了存储权限,我似乎也可以访问画廊。

【问题讨论】:

    标签: android-gallery android-6.0-marshmallow android-external-storage runtime-permissions


    【解决方案1】:

    您需要请求 READ_EXTERNAL_STORAGE。您可以在没有它的情况下访问图库,但如果您想对从图库中获得的媒体进行任何操作,则需要 READ 权限。

    快速测试从图库中选取图像后 onActivityResult 中发生的情况:

    权限拒绝:读取 com.android.providers.media.MediaProvider uri content://media/external/images/media 来自 pid=8405, uid=10177 需要 android.permission.READ_EXTERNAL_STORAGE,或 grantUriPermission()

    【讨论】:

      【解决方案2】:

      对于自定义权限,如果您使用的是 Android 6.0 或更高版本,则可以使用运行时权限。此代码可能会对您有所帮助。

      如果您的应用还没有所需的权限,则该应用必须 调用其中一个 requestPermissions() 方法来请求 适当的权限。您的应用传递了它想要的权限,并且 还有一个整数请求代码,您指定它来识别它 权限请求。此方法异步运行:它返回 立即,在用户响应对话框后,系统 使用结果调用应用程序的回调方法,传递相同的结果 应用传递给 requestPermissions() 的请求代码。

      // 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 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, 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.
          }
      }    
      

      To Know more about runtime permission

      https://developer.android.com/training/permissions/requesting.html

      【讨论】:

        猜你喜欢
        • 2017-02-25
        • 1970-01-01
        • 1970-01-01
        • 2017-07-05
        • 1970-01-01
        • 1970-01-01
        • 2021-10-05
        • 2016-03-25
        • 1970-01-01
        相关资源
        最近更新 更多