为什么我使用以下代码保存图片到相册里会提示权限错误: create asset failed with error: 13900012, without medialibrary permission
我的代码如下,我看官网里写这个是不需要手动获取权限的。
public static async saveToSystemPhoto(uiContext: common.Context, filePath: string, suffix?: string): Promise<void> {
try {
let context = uiContext;
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(context);
// 需要确保fileUri对应的资源存在
let fileUri = filePath
let assetChangeRequest: photoAccessHelper.MediaAssetChangeRequest = photoAccessHelper.MediaAssetChangeRequest.createImageAssetRequest(context, fileUri);
let promise = await phAccessHelper.applyChanges(assetChangeRequest);
console.info('createAsset successfully, uri: ' + assetChangeRequest.getAsset().uri);
return promise
} catch (err) {
console.error(`create asset failed with error: ${err.code}, ${err.message}`);
}
}
1、13900012报错是没有权限:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/errorcode-filemanagement-V5\#section13900012-拒绝许可
2、可以申请权限:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/restricted-permissions-V5\#section1417080131712
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/photoaccesshelper-preparation-V5\#%E7%94%B3%E8%AF%B7%E7%9B%B8%E5%86%8C%E7%AE%A1%E7%90%86%E6%A8%A1%E5%9D%97%E5%8A%9F%E8%83%BD%E7%9B%B8%E5%85%B3%E6%9D%83%E9%99%90
3、目前对于相册规范这块有相应管控,目前如果需要实现保存到相册,不符合受限权限场景的情况,只能通过SaveButton 安全控件实现,暂无其他方法。参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/restricted-permissions-V5\#section1417080131712
https://developer.huawei.com/consumer/cn/doc/harmonyos-faqs-V5/faqs-image-20-V5