简体   繁体   English

与 React Native 共享文件

[英]Share file with React Native

I use { Share } from 'react-native'.我使用来自“react-native”的{Share}。 I shared message successfully, no problem.我成功分享了消息,没问题。

Now, I generate dynamically a PDF and save it at local.现在,我动态生成一个 PDF 并将其保存在本地。 Is it possible to share the PDF like when I share an url?是否可以像我分享 url 一样分享 PDF? I didn't find solutions.我没有找到解决方案。

After thinking, if PDF isn't possible to share.想了想,要是PDF是不能分享的。 I have the idea to create dynamically HTML file with react native, then I can share the html file, but it's the same problem.我有想法用 react native 动态创建 HTML 文件,然后我可以共享 html 文件,但这是同样的问题。 There are no informations about sharing file with { Share}.没有关于使用 {Share} 共享文件的信息。

I think it's possible with the { Share } from 'react-native' because I saw the same thing on another app on Android.我认为来自“react-native”的 { Share } 是可能的,因为我在 Android 上的另一个应用程序上看到了同样的事情。

I had the same problem with the share of react native. 反应原生的份额我遇到了同样的问题。 I just used this library React Native Share . 我刚用这个库React Native Share

And you can pass the url to the file in your file system and it will work properly: 您可以将URL传递到文件系统中的文件,它将正常工作:

 Share.open({
                title: "This is my report ",
                message: "Message:",
                url: "file:///storage/emulated/0/demo/test.pdf",
                subject: "Report",
            })

This seems to work with the basic Share library from React Native, as long as you have the file:// URL of your PDF file: 这似乎与React Native的基本共享库一起使用,只要您拥有PDF文件的file:// URL:

Share.share({
  url: `file:///path/to/your/file.pdf`,
  title: 'Download PDF'
})

I've only tested on iOS, so YMMV. 我只在iOS上测试过,所以YMMV。

here is my code currently are used in my app for share image link:这是我的应用程序中当前用于共享图像链接的代码:

shareMessage =async (PhotoLink) => {
   try {
     const result = await Share.share({
       title:"title goes here",
       // url:,
       message: "see the photo"+JSON.stringify(PhotoLink),
       
       
     });
     if (result.action === Share.sharedAction) {
       if (result.activityType) {
         // shared with activity type of result.activityType
       } else {
         // shared
       }
     } else if (result.action === Share.dismissedAction) {
       // dismissed
     }
   } catch (error) {
     alert(error.message);
   }}
...
onPress={() => this.shareMessage(this.props.route.params.url)}

I had the same issue.我遇到过同样的问题。

Fixed it by adding file:// in-front of path.通过在路径前面添加file://来修复它。

 Share.open({
      title: `Share ${fileName}`,
      url: `file://${filePath}`,
      type: 'audio/mp3',
    })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM