react-native - 如何使用 moveFile react-native-fs
问题描述
我需要在 react native 应用程序上共享本地图像,使用 react-native share 和 react-native-fs。图像位于名为“图像”的根应用程序的本地文件夹中。如何分享这张图片。我是否需要将图像复制或移动到 temp 并使用该图像来获取绝对路径。
这是我的代码。rnfs.movefile 不起作用
getAssetFileAbsolutePath = async () => {
const dest =
`${RNFS.TemporaryDirectoryPath}${Math.random().toString(36)
.substring(
.7)}.png`;
const img = './images/page1.png';
try {
await RNFS.moveFile(img, dest);
console.log("dobro", dest)
} catch(err) {
console.log("greska", err)
}
}
我收到错误“page1.png”无法移动到“tmp”,因为前者不存在,或者包含后者的文件夹不存在。
解决方案
使用rn-fetch blob和 react native share 首先查找图像的路径并将其转换为 base64。然后分享图片使用反应原生分享包
ShareFile(file) {
let imagePath = null;
RNFetchBlob.config({
fileCache: true
})
.fetch("GET", file)
// the image is now dowloaded to device's storage
.then(resp => {
// the image path you can use it directly with Image component
imagePath = resp.path();
return resp.readFile("base64");
})
.then(async base64Data => {
var base64Data = `data:image/png;base64,` + base64Data;
// here's base64 encoded image
await Share.open({ url: base64Data });
// remove the file from storage
return fs.unlink(imagePath);
});
}
推荐阅读
- c# - 如何在 microsoft pos.net 中为不提供 SDK/驱动程序的品牌配置销售点硬件
- r - R 中的事件数据可视化,非常简单的问题 i
- html - 如何将我的表单文本框设置为默认字体?
- node.js - 使用节点 js 的 Twitter 直接消息
- javascript - Typescript 不会为名为“name”的未初始化对象属性触发错误
- javascript - 如何在 React 中设计一个复杂的表单
- javascript - 实现一个简单的替换方法,仍然得到大写的单词
- graph - Gremlin,得到两个彼此都有边的顶点
- go - 如何在 Go 中启用 CORS
- python - 如何在 Putty 关闭后保持 Django 项目运行?