我需要在react本机应用程序上共享本地镜像,使用react-native share和react- native -fs。图像位于名为“images”的根应用程序的本地文件夹中。如何分享这张图片。我是否需要将图像复制或移动到临时,并使用该图像获取绝对路径。
这是我的代码。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”,因为前者不存在,或者包含后者的文件夹不存在。
发布于 2019-10-07 21:21:36
将rn-fetch blob与react原生共享一起使用,首先找到镜像的路径,并将其转换为base64。然后使用react native share包共享镜像
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);
});
}https://stackoverflow.com/questions/58270283
复制相似问题