首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用moveFile react-native-fs

如何使用moveFile react-native-fs
EN

Stack Overflow用户
提问于 2019-10-07 21:11:54
回答 1查看 1.6K关注 0票数 0

我需要在react本机应用程序上共享本地镜像,使用react-native share和react- native -fs。图像位于名为“images”的根应用程序的本地文件夹中。如何分享这张图片。我是否需要将图像复制或移动到临时,并使用该图像获取绝对路径。

这是我的代码。rnfs.movefile不工作

代码语言:javascript
复制
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”,因为前者不存在,或者包含后者的文件夹不存在。

EN

回答 1

Stack Overflow用户

发布于 2019-10-07 21:21:36

rn-fetch blob与react原生共享一起使用,首先找到镜像的路径,并将其转换为base64。然后使用react native share包共享镜像

代码语言:javascript
复制
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);
    });
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58270283

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档