本文介绍: 参考:Android 13(API 33)读写SD卡权限的调整适配 – Mr,Han – 博客园。android获取文件读写权限(尤其是音视频图片等等资源)的权限获取逻辑更换了。android13 新权限获取

android13 新权限获取
参考:Android 13(API 33)读写SD卡权限的调整适配 – Mr,Han – 博客园
android获取文件读写权限(尤其是音视频、图片等等资源)的权限获取逻辑更换了。
必须申请如下权限:
在这里插入图片描述

   public static final String READ_MEDIA_AUDIO = "android.permission.READ_MEDIA_AUDIO";
   public static final String READ_MEDIA_IMAGES = "android.permission.READ_MEDIA_IMAGES";
   public static final String READ_MEDIA_VIDEO = "android.permission.READ_MEDIA_VIDEO";

这段逻辑可以看上面的文件拷贝类的代码

  /**
     *  权限检查
     * @param context
     * @return
     */
    private boolean hasStoragePermissions(Context context) {
        //版本判断,如果比android 13 就走正常的权限获取
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU){
            int readPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE);
            int writePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE);
            return readPermission == PackageManager.PERMISSION_GRANTED && writePermission == PackageManager.PERMISSION_GRANTED;
        }else{
            int audioPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_AUDIO);
            int imagePermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_IMAGES);
            int videoPermission = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_MEDIA_VIDEO);
            return audioPermission == PackageManager.PERMISSION_GRANTED && imagePermission == PackageManager.PERMISSION_GRANTED && videoPermission == PackageManager.PERMISSION_GRANTED;
        }
    }

    private void requestStoragePermissions(Context context) {
        String [] permissions;
        if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU){
            permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        }else{
            permissions = new String[]{Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.READ_MEDIA_IMAGES,Manifest.permission.READ_MEDIA_VIDEO};
        }
        ActivityCompat.requestPermissions((Activity) context,
                permissions,
                STORAGE_PERMISSION_REQUEST_CODE);
    }

原文地址:https://blog.csdn.net/weixin_44002043/article/details/134395271

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_37698.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注