Happy625
Happy625
  • 发布:2025-01-10 10:59
  • 更新:2025-01-10 11:03
  • 阅读:89

Android请求对所有文件的访问权限

分类:uni-app

清单文件添加文件读写权限

<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" />  
class FileUtils {  

          static checkPermission() {  
                const Build = plus.android.importClass('android.os.Build');  
                if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){  
                    const Environment =  plus.android.importClass("android.os.Environment");  
                    return Environment.isExternalStorageManager();  
                }  
                return true  
          }  

          static applyPermission(fn) {  
              const Build = plus.android.importClass('android.os.Build');  
              if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.R){  
                var Intent = plus.android.importClass("android.content.Intent");  
                var Settings = plus.android.importClass("android.provider.Settings");  
                var Uri = plus.android.importClass("android.net.Uri");  
                var mainActivity = plus.android.runtimeMainActivity();  

                var intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION);  
                var uri = Uri.fromParts("package", mainActivity.getPackageName(), null);  
                intent.setData(uri);  
                // intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
                mainActivity.startActivity(intent);   
              }  
              fn({state:'ok'})  
          }  
    }
if(FileUtils.checkPermission()==false){  
                    that.showLoading("请稍等")  
                    FileUtils.applyPermission(res => {  
                        uni.hideLoading()  
                    })  
                    return  
                }
0 关注 分享

要回复文章请先登录注册

Happy625

Happy625 (作者)

希望跟大家一起完整
2025-01-10 11:03