• 【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";
    
    • 1
    • 2
    • 3

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

      /**
         *  权限检查
         * @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);
        }
    
    • 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
  • 相关阅读:
    GAN反演+老照片修复
    玩客云 线刷Armbian 搭配Alist 阿里云盘 Jellyfin NovaVideoPlayer搞电视墙
    Java继承与多态
    Java线程的学习
    初识CSS
    人工智能-线性回归2--房价预测、欠拟合过拟合、正则化、模型保存加载
    二十、W5100S/W5500+RP2040树莓派Pico<MQTT连接阿里云控制板载LED>
    y139.第八章 Servless和Knative从入门到精通 -- 部署Knative(三)
    在深度学习中,累计不同批次的损失估计总体损失
    探索未来:大模型技术的最前沿
  • 原文地址:https://blog.csdn.net/weixin_44002043/article/details/134395271