uni.saveImageToPhotosAlbum(OBJECT)官方說明
平台差異說明:這個API不支持H5
| App | H5 | 微信小程序 | 支付寶小程序 | 百度小程序 | 字節跳動小程序、飛書小程序 | QQ小程序 | 快手小程序 |
|---|---|---|---|---|---|---|---|
| √ | x | √ | √ | √ | √ | √ | √ |
OBJECT 參數說明
| 參數名 | 類型 | 必填 | 說明 |
|---|---|---|---|
| filePath | String | 是 | 圖片文件路徑,可以是臨時文件路徑也可以是永久文件路徑,不支持網絡圖片路徑 |
| success | Function | 否 | 接口調用成功的回調函數 |
| fail | Function | 否 | 接口調用失敗的回調函數 |
| complete | Function | 否 | 接口調用結束的回調函數(調用成功、失敗都會執行) |
官方示例代碼:
//使用攝像頭拍攝照片獲取到臨時文件路徑保存
uni.chooseImage({ count: 1, sourceType: ['camera'], success: function (res) { uni.saveImageToPhotosAlbum({ filePath: res.tempFilePaths[0], success: function () { console.log('save success'); } }); } });
保存網絡圖片需要先使用uni.downloadFile 把網絡圖片資源下載到本地
savePhoto(){
uni.downloadFile({ //下載文件資源到本地,返回文件的本地臨時路徑
url: filePath, //網絡圖片路徑
success:(res)=>{
var imageUrl=res.tempFilePath;//臨時文件路徑
uni.saveImageToPhotosAlbum({ //保存圖片到系統相冊
filePath: imgUrl,
success: (res) => {
console.log('圖片保存成功');
},
fail: (err) => {
console.log('圖片保存失敗');
}
})
}
})
}
