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));
				      }
				    }
				  },
				});
}
相关推荐
lqj_本人1 小时前
鸿蒙OS&UniApp 实现一个精致的日历组件#三方框架 #Uniapp
uni-app·harmonyos
少恭写代码2 小时前
duxapp 2025-03-29 更新 编译结束的复制逻辑等
react native·小程序·taro
suncentwl3 小时前
答题pk小程序道具卡的获取与应用
小程序·答题小程序·知识竞赛
bysjlwdx3 小时前
uniapp婚纱预约小程序
小程序·uni-app
ALLSectorSorft3 小时前
代驾小程序订单系统框架搭建
小程序·代驾小程序
qq_12498707533 小时前
原生小程序+springboot+vue+协同过滤算法的音乐推荐系统(源码+论文+讲解+安装+部署+调试)
java·spring boot·后端·小程序·毕业设计·课程设计·协同过滤
骑450的皮卡丘10 小时前
uniapp设置 overflow:auto;右边不显示滚动条的问题
css·uni-app·css3
lqj_本人10 小时前
鸿蒙OS&UniApp实现个性化的搜索框与搜索历史记录#三方框架 #Uniapp
华为·uni-app·harmonyos
lqj_本人10 小时前
鸿蒙OS&UniApp制作多选框与单选框组件#三方框架 #Uniapp
前端·javascript·uni-app
前端极客探险家12 小时前
微信小程序全解析:从入门到实战
微信小程序·小程序