ios - 如何在 react-native 中获取绝对图像路径?
问题描述
我需要根据音频和图像创建视频。我在我的应用文件夹中保存了一张图片/src/img/test.jpg
。要创建视频,我需要一个绝对图像路径。
我试过了react-native-fs
。这是一些代码,我试图将图像从 src 路径复制到 DocumentDirectoryPath。
const dest = `${RNFS.DocumentDirectoryPath}/test.jpg`;
const path = 'src/img/test.jpg'
RNFS.copyFile(path, dest)
.then(() => FileViewer.open(dest))
.then(() => {
RNFS.readDir(RNFS.DocumentDirectoryPath)
.then((result) => {
console.log('GOT RESULT', result);
})
})
.catch(_err => {
console.log(_err);
});
我得到了错误:
Error: The file “test.jpg” couldn’t be opened because there is no such file.
解决方案
你无法访问
const path = 'src/img/test.jpg'
在运行时,因为它捆绑在您的应用程序中
尝试这个 :
const dest = `${RNFS.DocumentDirectoryPath}/test.jpg`;
const img = require('src/img/test.jpg');
RNFS.writeFile(dest, img, "base64")
.then(() => FileViewer.open(dest))
.then(() => {
RNFS.readDir(RNFS.DocumentDirectoryPath)
.then((result) => {
console.log('GOT RESULT', result);
})
})
.catch(_err => {
console.log(_err);
});
推荐阅读
- java - 具有无效字符的java xml
- visual-studio-2017 - 无法在 Visual Studio 2017 中调试
- windows - 批处理文件查找连接的 Citrix 服务器
- python-3.x - 使用 wForms 在 0x07808D68 处获取输出 .lazy_gettext 的 make_lazy_gettext>
- c# - c# 展平二维数组,获取二维,不循环
- elasticsearch - SearchGuard 社区版 - Rest API
- asp.net - 在经典的asp网站中添加cshtml页面。经典asp页面上的链接,点击时必须打开cshtml页面
- python - Django 框架:特定 pk 的对象不显示在网页上
- raspberry-pi - Electron - Linux - 黑屏
- c# - 从 C# 中的 pdf 文件中读取列虎钳数据并根据该列名插入到 sql 表中