The uniapp WeChat applet saves pictures to the system album (including access to mobile phone album permissions) and uses them immediately

1. Code, ready to use

const savePosterPath = (url) => {
    
    
	uni.showLoading({
    
    
		title: '正在保存图片...'
	});
	//获取用户的当前设置。获取相册权限
	uni.getSetting({
    
    
		success: (res) => {
    
    
			//如果没有相册权限
			if (!res.authSetting["scope.writePhotosAlbum"]) {
    
    
				//向用户发起授权请求
				uni.authorize({
    
    
					scope: "scope.writePhotosAlbum",
					success: () => {
    
    
						//授权成功保存图片到系统相册
						uni.saveImageToPhotosAlbum({
    
    
							//图片路径,不支持网络图片路径
							filePath: url,
							success: (res) => {
    
    
								uni.hideLoading();
								return uni.showToast({
    
    
									title: "保存成功!",
								});
							},
							fail: (res) => {
    
    
								console.log(res.errMsg);
								return uni.showToast({
    
    
									title: res.errMsg,
								});
							},
							complete: (res) => {
    
    uni.hideLoading();},
						});
					},
					//授权失败
					fail: () => {
    
    
						uni.hideLoading();
						uni.showModal({
    
    
							title: "您已拒绝获取相册权限",
							content: "是否进入权限管理,调整授权?",
							success: (res) => {
    
    
								if (res.confirm) {
    
    
			//调起客户端小程序设置界面,返回用户设置的操作结果。(重新让用户授权)
									uni.openSetting({
    
    
										success: (res) => {
    
    
										console.log(res.authSetting);
										},
									});
								} else if (res.cancel) {
    
    
									return uni.showToast({
    
    
										title: "已取消!",
									});
								}
							},
						});
					},
				});
			} else {
    
    
				//如果已有相册权限,直接保存图片到系统相册
				uni.saveImageToPhotosAlbum({
    
    
					filePath: url,
					success: (res) => {
    
    
						uni.hideLoading();
						return uni.showToast({
    
    
							title: "保存成功!",
						});
					},
					fail: (res) => {
    
    
						uni.hideLoading();
						console.log(res.errMsg);
						return uni.showToast({
    
    
							title: res.errMsg,
						});
					},
					//无论成功失败都走的回调
					complete: (res) => {
    
    uni.hideLoading();},
				});
			}
		},
		fail: (res) => {
    
    },
	});
};

2. Introduce the method used

1. uni.getSetting

uniapp official website link: https://uniapp.dcloud.io/api/other/setting.html#getsetting
insert image description here

2. uni.authorize

Link: https://uniapp.dcloud.io/api/other/authorize.html#authorize
insert image description here
insert image description here

3. uni.saveImageToPhotosAlbum

Link: https://uniapp.dcloud.io/api/media/image.html#saveimagetophotosalbum
insert image description here

4. uni.openSetting

Link: https://uniapp.dcloud.io/api/other/setting.html#opensetting
insert image description here

Guess you like

Origin blog.csdn.net/honeymoon_/article/details/123984876