OpenHarmony开发者论坛

标题: 如何申请设备上的媒体读写权限? [打印本页]

作者: xiagengwen    时间: 2023-8-28 18:53
标题: 如何申请设备上的媒体读写权限?
[md]【问题描述】

平时使用我们手机上的一些应用软件,会弹出提示框提示需要获取的权限。在OpenHarmony如何申请媒体的读写权限,并且在使用应用时弹出提示框显示提示?

【运行环境】

硬件:rk3568;  ROM:  3.2 Release ;  API 9
[/md]
作者: madixin    时间: 2023-8-29 14:59
在module.json5配置文件中配置媒体读写权限ohos.permission.READ_MEDIA和ohos.permission.WRITE_MEDIA。
示例:
  1. {
  2.   "module" : {
  3.     "requestPermissions":[
  4.       {
  5.         "name" : "ohos.permission.READ_MEDIA",
  6.         "reason": "$string:reason"
  7.       },
  8.       {
  9.         "name" : "ohos.permission.WRITE_MEDIA",
  10.         "reason": "$string:reason"
  11.       }
  12.     ]
  13.   }
  14. }
复制代码

这两个权限的授权方式均为user_grant,因此需要调用requestPermissionsFromUser接口,以动态弹窗的方式向用户申请授权
  1. let context = getContext(this) as common.UIAbilityContext;
  2. let atManager = abilityAccessCtrl.createAtManager();
  3. let permissions: Array<string> = ['ohos.permission.READ_MEDIA','ohos.permission.WRITE_MEDIA']
  4. atManager.requestPermissionsFromUser(context, permissions)
  5. .then((data) => {
  6.     console.log("Succeed to request permission from user with data: " + JSON.stringify(data))
  7. }).catch((error) => {
  8.     console.log("Failed to request permission from user with error: " + JSON.stringify(error))
  9. })
复制代码





欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/) Powered by Discuz! X3.5