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));
				      }
				    }
				  },
				});
}
相关推荐
云起SAAS7 小时前
倒班日历助手抖音快手微信小程序看广告流量主开源
微信小程序·小程序·ai编程·看广告变现轻·倒班日历助手
sheji34169 小时前
【开题答辩全过程】以 基于微信小程序的失物认领系统为例,包含答辩的问题和答案
微信小程序·小程序
qq_25684788869 小时前
08cms房产多城市版最新v8.7含小程序及装修网的安装及配置方法整理出来希望能帮到大家
小程序·08cms房产系统源码·08cms装饰网门户系统·08cms 房产门户系统源码
风月歌10 小时前
python项目之摄影竞赛小程序
python·mysql·小程序·毕业设计·源码
云云只是个程序马喽10 小时前
2026年短剧系统开发搭建全流程教程(小程序+APP自营/红果模式/广告联盟/海外多语言)
小程序
Rysxt_10 小时前
uni-app 使用教程:从入门到发布
uni-app
计算机毕设指导610 小时前
基于微信小程序的网络安全知识科普平台系统【源码文末联系】
java·spring boot·安全·web安全·微信小程序·小程序·tomcat
天***88961 天前
小程序婚纱店摄影楼预约婚庆礼展示,小程序开发定制,会员下单档期系统
小程序
说私域1 天前
开源AI智能名片链动2+1模式商城小程序下短视频电商变现与广告变现的对比研究
人工智能·小程序
homelook1 天前
uniapp蓝牙demo
uni-app