react-native - 如何在本机反应中将捕获的图像保存到应用程序文件夹?
问题描述
我是反应原生的新手我正在使用反应原生相机库来捕获图像。我现在已经捕获了图像我希望它保存在应用程序文件夹中,例如资产或 src 文件夹。我有图像 uri。我该怎么做请让我知道。
state = {
cameraType : 'back',
mirrorMode : false,
path:null
}
<View>
<RNCamera
ref={ref => {
this.camera = ref;
}}
mirrorImage={this.state.mirrorMode}
style={styles.preview}
type={RNCamera.Constants.Type.front}
flashMode={RNCamera.Constants.FlashMode.on}
androidCameraPermissionOptions={{
title: 'Permission to use camera',
message: 'We need your permission to use your camera',
buttonPositive: 'Ok',
buttonNegative: 'Cancel',
}}
androidRecordAudioPermissionOptions={{
title: 'Permission to use audio recording',
message: 'We need your permission to use your audio',
buttonPositive: 'Ok',
buttonNegative: 'Cancel',
}}
/>
<Button onPress={() => this.takePicture()} style={styles.btncontainer}>
<Text>Take Picture</Text>
</Button>
</View>
takePicture = async() => {
if (this.camera) {
const options = { quality: 0.5, base64: true };
const data = await this.camera.takePictureAsync(options);
this.setState({ path: data.uri });
console.log("url",this.state.path)
}
};
console.log url file:///data/user/0/com.tracking/cache/Camera/231f7d30-f74c-4f6d-b955-284b108592ca.jpg
解决方案
您可以使用react-native-fs。
const moveAttachment = async (filePath, newFilepath) => {
return new Promise((resolve, reject) => {
RNFS.mkdir(dirPicutures)
.then(() => {
RNFS.moveFile(filePath, newFilepath)
.then(() => {
console.log('FILE MOVED', filePath, newFilepath);
resolve(true);
})
.catch(error => {
console.log('moveFile error', error);
reject(error);
});
})
.catch(err => {
console.log('mkdir error', err);
reject(err);
});
});
};
推荐阅读
- python - 如何部署对托管在 pythonanywhere 上的 django 项目所做的更改?
- javascript - 在前端重用后端模块
- javascript - Firebase 实时数据库安全性,仅使用时间戳读取过去 10 分钟的消息不起作用
- json - 使用 Swift 一次将多个字典写入 Firebase 实时数据库,使用 childByAutoId()
- javascript - 反应切换在初始状态更改时返回错误状态
- c# - UriFormatException:无效的 URI:无法解析主机名。MVC API C#
- r - 是否有可能在 r 中使用 dynlm 对象执行重置测试
- php - 如何在查询中绑定参数数组和值
- linux - 在 bash 脚本中获取相对简单的正则表达式以匹配的问题
- assembly - XNZVC 操作(汇编语言 Easy68K)