如何在本机反应中将捕获的图像保存到应用程序文件夹?
Posted
技术标签:
【中文标题】如何在本机反应中将捕获的图像保存到应用程序文件夹?【英文标题】:how to save captured image to application folder in react native? 【发布时间】:2020-03-26 13:16:29 【问题描述】:我是 react native 的新手我正在使用 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
【问题讨论】:
【参考方案1】:您可以使用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);
);
);
;
【讨论】:
以上是关于如何在本机反应中将捕获的图像保存到应用程序文件夹?的主要内容,如果未能解决你的问题,请参考以下文章