ReactNative圖片保存到相冊(cè)

ios 保存:
React Native 的 CameraRoll API 提供了訪問本地相冊(cè)的功能(經(jīng)過測(cè)試該組件只支持 ios)

import {Platform,CameraRoll} from 'react-native';
 var promise = CameraRoll.saveToCameraRoll("圖片的 url")
            promise.then(function(result) {
                this.refs.toast.show("圖片已保存至相冊(cè)")
            }).catch(function(error) {
                this.refs.toast.show("保存失敗")
            })

Android 的保存利用 react-native-fs 進(jìn)行保存圖片

const RNFS = require('react-native-fs') //文件處理
const storeLocation = `${RNFS.DocumentDirectoryPath}`
// 安卓上傳圖片
function sendImgAndroid(url,succeedFun,errerFn){
  console.log(url)
  let pathName = new Date().getTime() + "xg.png"
  let downloadDest = `${storeLocation}/${pathName}`;
  const ret = RNFS.downloadFile({fromUrl:url,toFile:downloadDest});
  ret.promise.then(res => {
    if(res && res.statusCode === 200){
      var promise = CameraRoll.saveToCameraRoll("file://" + downloadDest);
      promise.then(function(result) {
        // 保存成功
        console.log('圖片保存成功')
        succeedFun(result)
      }).catch(function(error) {
        // 保存失敗
        console.log('圖片保存失敗')
        errerFn(error)
      })
    }
  })
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容