gpt4 book ai didi

android - 在运行时请求和允许 WRITE_EXTERNAL_STORAGE 权限对当前 session 没有影响

转载 作者:IT老高 更新时间:2023-10-28 22:14:59 30 4
gpt4 key购买 nike

这是关于 runtime permissions introduced in Android Marshmallow 的新型号请求 Manifest.permission.WRITE_EXTERNAL_STORAGE 权限时。

简而言之,我遇到的是,如果我请求(并且用户允许)Manifest.permission.WRITE_EXTERNAL_STORAGE 权限,应用程序将无法从外部存储读取和写入目录直到我销毁并重新启动应用程序

这就是我正在做/正在经历的事情:

我的应用从以下状态开始:

ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED

也就是说,我没有访问外部存储的权限。

然后,正如 Google 解释的那样,我请求 Manifest.permission.WRITE_EXTERNAL_STORAGE 的权限

private void requestWriteExternalStoragePermission() {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
new AlertDialog.Builder(this)
.setTitle("Inform and request")
.setMessage("You need to enable permissions, bla bla bla")
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(MendeleyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_PERMISSION_WRITE_EXTERNAL_STORAGE);
}
})
.show();
} else {
ActivityCompat.requestPermissions(MendeleyActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, RC_PERMISSION_WRITE_EXTERNAL_STORAGE);
}
}

一旦用户允许该权限,onRequestPermissionsResult 就会被调用。

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case RC_PERMISSION_WRITE_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && PackageManager.PERMISSION_GRANTED
// allowed
} else {
// denied
}
break;
}
}
}

allowed block 被执行,确认用户已授予权限。

立即在此之后,如果我不销毁并再次打开应用程序,我仍然没有访问外部存储的权限。更具体地说:

hasWriteExternalStoragePermission();                  // returns true
Environment.getExternalStorageDirectory().canRead(); // RETURNS FALSE!!
Environment.getExternalStorageDirectory().canWrite(); // RETURNS FALSE!!

所以,Android 运行时似乎认为我有权限,但文件系统没有...实际上,尝试访问 Environment.getExternalStorageDirectory() 会引发异常:

android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 
at java.io.FileOutputStream.<init>(FileOutputStream.java:72) 

如果我现在销毁应用程序并再次打开它,行为将变为应有的状态,能够在外部存储文件夹中读取和写入。

有人遇到这种情况吗?

我正在使用一个官方模拟器:

  • 最新的 Android 6.0 (API 23) API 23, Rev 1。
  • 运行 Intel x86 Atom 系统镜像、API 23、Rev 1 的模拟器。

我使用以下方式构建应用程序:

android {
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
minSdkVersion 16
targetSdkVersion 23
}
...
}

如果有人证实了这一点,而且我不是唯一一个我想我们需要打开一个错误,但我希望我做错了什么,因为我认为这样的核心功能不太可能在 SDK 中出现错误。

最佳答案

http://developer.android.com/reference/android/Manifest.permission.html#WRITE_EXTERNAL_STORAGE :

Starting in API level 19, this permission is not required to read/write files in your application-specific directories returned by getExternalFilesDir(String) and getExternalCacheDir().

运行时权限从 API 级别 23 开始,显然高于 19,因此不再需要该权限,除非您访问 getExternalFilesDir() 指向的文件夹之外的数据。因此,我认为这是一个仅在模拟器上测试时才会出现的错误。

19级以下的target,不支持在运行时请求权限,只要在manifest中声明权限即可。

关于android - 在运行时请求和允许 WRITE_EXTERNAL_STORAGE 权限对当前 session 没有影响,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32947638/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com