0%

安卓11获取文件读写权限代码

  • 英语:

安卓11改变了此前安卓系统对于文件管理的规则,在安卓11上,文件读写变成了特殊权限,需要将相关说明通过 Activity 展现给用户看,之前的申请权限代码(包括安卓10)全部失效。采用旧的申请代码无法成功的读写文件。因此这里列出新的方法,以作备份。

  1. build.gradle中升级 targetSdkVersion

    1
    2
    3
    4
    5
    android 
    defaultConfig {
    targetSdkVersion 30
    }
    }
  2. AndroidManifest.xml中添加清单权限

    1
    2
    3
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
  3. 动态请求权限

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestPermission(this);
    ......
    }

    private void requestPermission(Context context){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    // 先判断有没有权限
    if (Environment.isExternalStorageManager()) {
    writeFile();
    } else {
    Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);
    intent.setData(Uri.parse("package:" + context.getPackageName()));
    startActivityForResult(intent, REQUEST_CODE);
    }
    } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    // 先判断有没有权限
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
    ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    writeFile();
    } else {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
    }
    } else {
    writeFile();
    }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_CODE) {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED &&
    ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
    writeFile();
    } else {
    ToastUtils.show("存储权限获取失败");
    }
    }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQUEST_CODE && Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
    if (Environment.isExternalStorageManager()) {
    writeFile();
    } else {
    ToastUtils.show("存储权限获取失败");
    }
    }
    }

    /**
    * 模拟文件写入
    */
    private void writeFile() {
    Toast.makeText(this, "可以写入文件了", Toast.LENGTH_SHORT).show();
    }
参考

Android 11 存储权限适配指南及方案

-------全文结束  感谢您的阅读-------
  • 本文作者: LuoV
  • 本文链接: https://xian6ge.cn/posts/eed7c55e/
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!