Android 6运行时权限访问图库

5

我有关于Android 6(棉花糖)运行时权限的问题。如果用户想要从图库选择一张照片,我们应该请求READ_EXTERNAL_STORAGE权限吗?

似乎即使我关闭存储权限,也可以访问图库。

2个回答

4

您需要请求READ_EXTERNAL_STORAGE权限。没有该权限,您仍然可以访问图库,但如果您想对从图库获取的媒体进行任何操作,您将需要READ权限。

在从图库选择图片后,onActivityResult中会发生什么的快速测试:

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


0

如果您正在使用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.
    }
}    

了解有关运行时权限的更多信息。

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接