鸿蒙开发api9及以上之相册权限、相机权限、文件读写权限申请的正确方法

发布时间:2024年01月17日

1.在module.json5中注册权限信息

和"abilities": []同级别

    "requestPermissions": [
{
  "name": "ohos.permission.READ_MEDIA",
  "reason": "$string:EntryAbility1_desc",
  "usedScene": {
    "abilities": [
      "EntryAbility"
    ],
    "when": "inuse"
  }
},
      {
        "name": "ohos.permission.WRITE_MEDIA",
        "reason": "$string:EntryAbility1_desc",
        "usedScene": {
          "abilities": [
            "EntryAbility"
          ],
          "when": "inuse"
        }
      },
      {
        "name": "ohos.permission.MEDIA_LOCATION",
        "reason": "$string:EntryAbility1_desc",
        "usedScene": {
          "abilities": [
            "EntryAbility"
          ],
          "when": "inuse"
        }
      }
    ]
"ohos.permission.READ_MEDIA" 文件读权限
"ohos.permission.WRITE_MEDIA"文件写权限
"ohos.permission.MEDIA_LOCATION" 文件位置权限: 允许应用访问用户媒体文件中的地理位置信息。

2.然后在需要调用的地方

获取当前context

  context2:common.UIAbilityContext = getContext(this) as common.UIAbilityContext

3.声明权限数组

const permissions: Array<Permissions> = [
  'ohos.permission.READ_MEDIA',
  'ohos.permission.WRITE_MEDIA',
  'ohos.permission.MEDIA_LOCATION',
];

4.最后才是代码调用

const atManager = abilityAccessCtrl.createAtManager();


atManager.requestPermissionsFromUser(this.context2, permissions, (err, data) => {
 if (!err) {
   
 } else {
                  
               
 }
            
 });

文章来源:https://blog.csdn.net/qq_15509071/article/details/135659048
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。