uniapp 写抖音小程序--下载图片时弹起保存到相册授权框用uni.authorize有时不生效的情况

uniapp 写抖音小程序–下载图片时弹起保存到相册授权框用uni.authorize有时不生效的情况

想要的结果,点击图片下载保存到相册时弹起授权框

在这里插入图片描述
最主要的就是需要弹出一次这个授权框,因为只要弹出一次这个授权框在权限设置页面才会有授权选项,如果没有弹起过这个授权框,那么权限设置页面也不会有这个选项就会进入一个死循环,永远打不开这个授权弹框
在这里插入图片描述
一般思路会在点击下载时就去获取判断是否有保存到相册的权限,如果没有就直接去唤起授权框

uni.authorize({
    scope: 'scope.album',
    success() {
        //
    }
})

这个思路正常,但是又稍稍有点问题。。。。。。。。。
因为代码uni.authorize并不会主动唤起授权框,反而在uni.saveImageToPhotosAlbum(保存图片到相册)会自动唤起授权框,所以思路倒个位置------先去调用uni.saveImageToPhotosAlbum(保存图片到相册),弹起授权框(这里不管点击同意还是拒绝都会保存到权限设置的页面里),这样就方便了后续的操作↓↓↓
一个小demo的源码,只有一个页面;如果有别的功能请自行添加~

<template>
	<view class="pages">
		<view class="pages-top">
			<text class="title">{{title}}</text>
		</view>
		<view class="pages-content">
			<view v-for="(item,index) in imgList">
				<image class="logo" :src="item.img_url" @click="downLoad(item.img_url)"></image>
			</view>
		</view>
	</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '下载图片获取相册权限',
				imgList: [{
						id: 1,
						img_url: 'https://www.itying.com/images/flutter/1.png'
					},
					{
						id: 2,
						img_url: 'https://www.itying.com/images/flutter/2.png'
					},
					{
						id: 3,
						img_url: 'https://www.itying.com/images/flutter/3.png'
					},
					{
						id: 4,
						img_url: 'https://www.itying.com/images/flutter/4.png'
					},
					{
						id: 5,
						img_url: 'https://www.itying.com/images/flutter/5.png'
					},
					{
						id: 6,
						img_url: 'https://www.itying.com/images/flutter/6.png'
					}, {
						id: 7,
						img_url: 'https://www.itying.com/images/flutter/7.png'
					}
				]
			}
		},
		onLoad() {

		},
		methods: {
			downLoad(img_url) {
				// 下载文件接口
				uni.downloadFile({
					url: img_url,
					success: (res) => {
						if (res.statusCode === 200) {
							uni.showLoading({
								title: "正在保存..."
							})
							//保存图片至相册
							uni.saveImageToPhotosAlbum({
								filePath: res.tempFilePath,
								success: () => {
									uni.hideLoading();
									uni.showToast({
										title: "保存成功",
										icon: "success"
									});
								},
								// 可能是点击取消,也可能是关闭权限保存失败
								fail: (err) => {
									uni.hideLoading();
									// 10200:权限被拒绝
									if (err.errNo == 10200) {
										uni.showModal({
											content: '由于您还没有允许保存图片到您相册里,无法进行保存,请点击确定允许授权',
											success: (res) => {
												if (res.confirm) {
													this.checkAuthor()
												}
											}
										});
									}
									// 10502:用户点击取消
									if (err.errNo == 10502) {
										uni.showToast({
											title: "点击取消,保存失败!",
											icon: "fail"
										});
									}
								}
							});
						}
					},
					fail: (err) => {
						uni.showToast({
							title: "文件下载失败,请检查资源路径是否正确!",
							icon: "fail"
						});
					}
				})
			},

			checkAuthor() {
				uni.getSetting({
					success: (result) => {
						if (!result.authSetting['scope.album']) {
							uni.openSetting({
								success: (
									result
								) => {
									if (result.authSetting[
											'scope.album']) {
										this.downLoad()
									} else {
										uni.showToast({
											title: "请在弹出的权限设置页面打开相册权限!",
											icon: "fail"
										});
									}
								}
							});
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.pages{
		padding: 30rpx;
		.pages-top{
			text-align: center;
			.title{
				font-size: 36rpx;
				color: #8f8f94;
			}
		}
		.pages-content{
			margin-top: 20rpx;
			display: flex;
			justify-content: space-between;
			flex-wrap: wrap;
			.logo{
				margin: 10rpx 0;
				width: 220rpx;
				height: 220rpx;
			}
		}
	}
</style>
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值