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

时间:2025-04-04 13:59:11

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

和"abilities": []同级别

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

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

获取当前context

  context2: = getContext(this) as 

3.声明权限数组

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

4.最后才是代码调用

const atManager = ();


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