本文主要是介绍鸿蒙开发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<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 {}});
这篇关于鸿蒙开发api9及以上之相册权限、相机权限、文件读写权限申请的正确方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!