uniapp图片保存到相册

  1. 你需要知道当前用户是否授权 你保存相册的权限
    2.你直接吧代码copy吧。 写不动了
savePoster()let _this=  this;
						uni.getSetting({
    
    
								success(res) {
    
    
							if (res.authSetting['scope.writePhotosAlbum']) {
    
    
											// 如果开启直接保存
										_this.saveImageToPhotosAlbum(_this.goodUrl);
									} else {
    
    
									uni.authorize({
    
     //如果没有授权,向用户发起请求
											scope: 'scope.writePhotosAlbum',
											success(res) {
    
    
												if (res.errMsg === 'authorize:ok') {
    
    
													_this.saveImageToPhotosAlbum(_this.goodUrl);
													}
												},
												fail() {
    
    
										uni.showToast({
    
    
										title: "请打开保存相册权限,再点击保存相册分享",
											icon: "none",
												duration: 3000
														});
													}
												})
											}
									},
							})
					},
		saveImageToPhotosAlbum(goodUrl) {
    
    
						// 这个是文件下载,返回对应base64还是什么格式的数据
						uni.downloadFile({
    
    
							url: goodUrl,
							success: (res) => {
    
    
								if (res.statusCode === 200) {
    
    
								// 这里保存开始了
									uni.saveImageToPhotosAlbum({
    
    
										filePath: res.tempFilePath,
										success() {
    
    
											uni.showToast({
    
    
												title: "保存成功",
												icon: "none"
											});
										},
										fail() {
    
    
											uni.showToast({
    
    
												title: "保存取消",
												icon: "none"
											});
										}
									});
								}
							}
						})
		}

猜你喜欢

转载自blog.csdn.net/weixin_45932463/article/details/120066985