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

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

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'
							});
						}
					}
				});
			},

第二种解决方案:
直接询问权限

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));
				      }
				    }
				  },
				});
}
  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值