android native 相册,ReactNative图片保存到相册(Android 和 ios)

ios 保存:

React Native 的 CameraRoll API 提供了访问本地相册的功能(经过测试该组件只支持 ios)

第一步

如果该库包含原生代码,那么在它的文件夹下一定有一个.xcodeproj文件。 把这个文件拖到你的XCode工程下(通常拖到XCode的Libraries分组里)

6242ec32f75f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

第二步

点击你的主工程文件,选择Build Phases,然后把刚才所添加进去的.xcodeproj下的Products文件夹中的静态库文件(.a文件),拖到Link Binary With Libraries组内。

6242ec32f75f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

第三步:

然后添加库所在的目录(如果它还有像React这样的子目录需要包含,注意要选中recursive选项)

6242ec32f75f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

代码使用:

import {Platform,CameraRoll} from 'react-native';

var promise = CameraRoll.saveToCameraRoll("图片的 url")

promise.then(function(result) {

this.refs.toast.show("图片已保存至相册")

}).catch(function(error) {

this.refs.toast.show("保存失败")

})

Android 保存:

Android 的保存使用CameraRoll配合 react-native-fs 进行实现保存图片

具体实现代码:

const RNFS = require('react-native-fs'); //文件处理

const storeLocation = `${RNFS.DocumentDirectoryPath}`;

let pathName = new Date().getTime() + "文件名.png"

let downloadDest = `${storeLocation}/${pathName}`;

const ret = RNFS.downloadFile({fromUrl:saveImageUrl,toFile:downloadDest});

ret.promise.then(res => {

if(res && res.statusCode === 200){

var promise = CameraRoll.saveToCameraRoll("file://" + downloadDest);

promise.then(function(result) {

console.log("图片已保存至相册")

}).catch(function(error) {

console.log("保存失败")

})

}

})

别忘加权限:

ok 大功告成

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值