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));
				      }
				    }
				  },
				});
}
相关推荐
疯狂的沙粒2 小时前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
Uyker3 小时前
从零开始制作小程序简单概述
前端·微信小程序·小程序
狼性书生13 小时前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
说私域13 小时前
定制开发开源AI智能名片驱动下的海报工厂S2B2C商城小程序运营策略——基于社群口碑传播与子市场细分的实证研究
人工智能·小程序·开源·零售
说私域20 小时前
内容力重塑品牌增长:开源AI大模型驱动下的智能名片与S2B2C商城赋能抖音生态种草范式
人工智能·小程序·开源·零售
Jiaberrr20 小时前
uniapp 安卓 APP 后台持续运行(保活)的尝试办法
android·前端·javascript·uni-app·app·保活
不老刘20 小时前
uniapp+vue3实现CK通信协议(基于jjc-tcpTools)
前端·javascript·uni-app
疯狂的沙粒1 天前
uni-app 如何实现选择和上传非图像、视频文件?
前端·javascript·uni-app
^Rocky1 天前
uniapp 集成腾讯云 IM 富媒体消息(地理位置/文件)
uni-app·腾讯云·媒体
$程1 天前
Uniapp 二维码生成与解析完整教程
前端·uni-app