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

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 = [
  '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 {
                  
               
 }
            
 });

你可能感兴趣的:(harmonyos,数码相机,华为)