react-native - React Native 将捕获的图像和视频保存到我设备上的自定义文件夹中
问题描述
我试图为我研究将捕获的图像或视频保存到设备上的自定义文件夹的正确答案,但没有看到合适的答案。我已经能够保存到我的 DCIM,但我不想将它们保存在那里,我想创建一个自定义文件夹来保存从我的应用程序中捕获的图像或视频。我是新来的反应本地人,这是我的学习过程......
takePicture = async () => {
if (this.camera) {
if (Platform.OS === 'android') {
await this.checkAndroidPermission();
}
const options = { quality: 1 };
const data = await this.camera.takePictureAsync(options);
//save photo
CameraRoll.save(data.uri, 'photo').then(onfulfilled => {
ToastAndroid.show(`VidApp Photos: ${onfulfilled}`, ToastAndroid.SHORT);
}).catch(error => {
ToastAndroid.show(`${error.message}`, ToastAndroid.SHORT);
});
}
};
recordVideo = async () => {
if (this.camera) {
if (!this.state.recording)
this.startRecording();
else this.stopRecording();
}
}
startRecording = async () => {
this.setState({ recording: true });
this.countRecordTime = setInterval(() => this.setState({ seconds: this.state.seconds + 1 }), 1000);
const cameraConfig = { maxDuration: this.state.maxDuration };
const data = await this.camera.recordAsync(cameraConfig);
this.setState({ recording: false });
CameraRoll.save(data.uri, 'video').then(onfulfilled => {
ToastAndroid.show(`VidApp Videos: ${onfulfilled}`, ToastAndroid.SHORT)
}).catch(error => ToastAndroid.show(`${error.message}`, ToastAndroid.SHORT));
}
stopRecording = () => {
this.camera.stopRecording();
clearInterval(this.countRecordTime);
this.setState({ seconds: 0 });
解决方案
您必须使用 CameraRoll.save 的专辑参数
CameraRoll.save(data.uri, {type:'photo',album:'CustomFolder'});
从文档
它允许在提供参数专辑时指定要将资产存储到的特定专辑。在 Android 上,如果没有提供专辑,则使用 DCIM 目录,否则根据提供的类型使用 PICTURE 或 MOVIES 目录。
推荐阅读
- java - Discord Bot (JDA) 速率限制
- python - BeautifulSoup(req.text, 'lxml') 输出 UnicodeEncodeError: 'charmap' codec can't encode character '\xe9'
- function - Common Lisp:是否有带有类型说明符的“标签”版本?
- python - 如何编写一个改变 DataFrame 列顺序的函数?(Python)
- 3d - 从 glTF 2.0 文件中删除未使用的对象
- node.js - DynamoDB 在 AWS Lambda 上始终返回“响应:null”
- javascript - 如何从 laravel 中的 jquery 下拉列表中捕获选定的数据
- regex - 使用 re2 正则表达式和匹配大小写否定字符串模式
- html - Bootstrap3中带有按钮工具栏的标题文本居中
- python - 将 pandas 数据帧旋转为具有多层的长格式