Uni app 开发支付宝小程序,保存到相册,获取小程序权限列表失败问题记录及解决方案。

第一种实现方案思路:先获取所有用户的权限列表配置,进行判断是否可以访问相册。

我在做的时候,在测试环境可以获取权限列表,但是在正式环境就会报错,报错原因是获取不到当前用户的权限列表用的这个方法(uni.getSetting),研究了两天也不知道为什么不行,支付宝开发者中心配置该配的也都配置了。于是就舍弃了第一种方案。用了第二种,直接向用户访问申请相册权限。就解决问题了。现在还是不知道为什么在更换了环境和模板之后调用权限列表会失效,如果有哪位小同学知道可以留言相互讨论一下。

javascript 复制代码
saveAlbum() {
                   uni.getSetting({ //获取用户的当前设置
					success: (res) => {
						console.log("获取权限", res);
						// res.authSetting.album && 
						if (res.authSetting.album || res.authSetting.camera ||  res.authSetting.writePhotosAlbum  ) { //验证用户是否授权可以访问相册
							this.saveNetImageToLocal();
						} else {
							
							uni.authorize({ //如果没有授权,向用户发起请求
								scope: 'scope.writePhotosAlbum',
								success: () => {
									uni.hideLoading();
									this.saveNetImageToLocal();
								},
								fail: (errr) => {
									console.log(errr)
									uni.showToast({
										title: errr.errorMessage,
										icon: "none",
										duration: 3000
									});
									uni.showToast({
										title: "请打开保存相册权限,再点击保存相册分享",
										icon: "none",
										duration: 3000
									});
									setTimeout(() => {
										console.log('哈哈哈哈')
										uni.openSetting({ //调起客户端小程序设置界面,让用户开启访问相册
											success: (res2) => {
												uni.hideLoading();
												console.log(res2)
											},
											fail(res33) {
												console.log(res33)
											}
										});
									}, 3000);
								}
							})
						}
					}
				})
},
saveNetImageToLocal() {
				let that = this;
				uni.downloadFile({
					url: that.imgUrl,
					success: (res) => {
						if (res.tempFilePath != '' && res.tempFilePath != null) {
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: (res) => {
									uni.hideLoading();
									uni.showToast({
										title: "保存成功!",
										icon: 'none'
									});
								},
								fail: (res) => {
									uni.hideLoading();
									uni.showToast({
										title: res.errMsg,
										icon: 'none'
									});
								},
								//无论成功失败都走的回调
								complete: (res) => {
									uni.hideLoading();
								},
							});
						} else {
							uni.showToast({
								title: '网络错误',
								icon: 'none'
							});
						}
					}
				});
			},

第二种解决方案:

直接询问权限

javascript 复制代码
saveAlbum(){
my.saveImageToPhotosAlbum ({
				  filePath: this.imgUrl,
				  success (res) {
				    console.log ('saveImageToPhotosAlbum 调用成功', res);
				  },
				  fail (err) {
				    console.log ('saveImageToPhotosAlbum 调用失败', err);
				    const platform = my.env.platform;
				    if (err.error === 15) {
				      // 提示用户开启相册权限
				      if (platform === 'iOS') {
				        my.showAuthGuide ({
				          authType: 'PHOTO',
				          complete (res) {
				            if (res.shown) {
				              console.log ('已展示权限引导');
				            } else {
				              console.error('保存图片失败: ', '请在系统设置中为支付宝并开启相册权限', JSON.stringify(err));
				            }
				          },
				        });
				      } else if (platform === 'Android') {
				        console.error('保存图片失败: ', '请在系统设置找到支付宝应用并开启文件和多媒体写入权限', JSON.stringify(err));
				      }
				    }
				  },
				});
}
相关推荐
MoonMoonLee5 天前
小程序配置
小程序
码兄科技5 天前
24小时自助健身房系统软件开发实战指南:功能设计与部署方案
java·spring boot·uni-app
2501_916007475 天前
苹果商店iOS应用上架费用全面解析:开发者计划与审核费用计算
android·ios·小程序·https·uni-app·iphone·webview
JQweryuiop5 天前
中小型游戏陪练工作室数字化管理实践:基于七彩屋电竞护航小程序订单、履约与绩效系统的落地复盘
大数据·游戏·小程序·架构
Ai-_Man5 天前
您您这可以把Mistral AI的多个会话比如说。左侧的多个会话一次性导出吗?不是单条会话里面的多次会对话。
人工智能·ai·小程序·电脑
2501_916008895 天前
如何实现iOS App代码混淆:SwiftShield使用指南
android·ios·小程序·https·uni-app·iphone·webview
微擎应用市场5 天前
简单拼车小程序系统:实现出行与物流资源的高效精准匹配
小程序
2501_916008895 天前
怎么用 Godot 导出 iOS 应用并上架 App Store?签名字段该填什么?
android·ios·小程序·https·uni-app·iphone·webview
2601_963870205 天前
基于springboot的综合网上购物商场系统
微信小程序·小程序·课程设计
2601_963869955 天前
力序健身房管理系统
微信小程序·小程序