react-native-share: Unable to share local file on Android

Steps to reproduce

Here is my function call to share.

 var sharingURL =
      "file:///data/user/0/com.myApp/files/ji_1560855618.txt";
    console.log("opening url " + sharingURL);
    Share.open({
      title: "React Native",
      message: "Hola mundo",
      url: sharingURL,
      subject: "Share Link" //  for email
    });

I also check permissions before calling share and it says it is granted

async askPermission() {
    console.log("asking permission");
    const granted = await PermissionsAndroid.check(
      "android.permission.READ_EXTERNAL_STORAGE"
    );
    if (!granted) {
      console.log("Permission not granted");
      const response = await PermissionsAndroid.request(
        "android.permission.READ_EXTERNAL_STORAGE"
      );
      if (!response) {
        console.log("Permission not granted & non respinse");
        return;
      }
    } else {
      console.log("Permission granted");
    }
  }

Expected behaviour

Tell us what should happen The file to be attached to an email

Actual behaviour

Tell us what happens instead No file is attached

Environment

    System:
      OS: macOS 10.14.6
      CPU: (4) x64 Intel(R) Core(TM) i5-7267U CPU @ 3.10GHz
      Memory: 16.07 MB / 16.00 GB
      Shell: 3.2.57 - /bin/bash
    Binaries:
      Node: 10.15.3 - /usr/local/bin/node
      Yarn: 1.13.0 - /usr/local/bin/yarn
      npm: 6.4.1 - /usr/local/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
    IDEs:
      Android Studio: 3.4 AI-183.5429.30.34.5452501
      Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
    npmPackages:
      react: 16.8.3 => 16.8.3 
      react-native: ^0.59.4 => 0.59.4 
    npmGlobalPackages:
      create-react-native-app: 2.0.2
      react-native-cli: 2.0.1

react-native-share

Version: npm version or “master”

"react-native-share": "git+https://git@github.com/react-native-community/react-native-share.git",

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 2
  • Comments: 27 (3 by maintainers)

Most upvoted comments

I added <uses-permission tools:node="remove" android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> In manifestFile. After removing it, share is working fine for me.

I managed to solve this issue by passing the pdf file path to the url param from sharing options. sharePDf = async (pdfPath) => { //Be sure that PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE is already granted const shareOptions = {

    url: 'file://'+pdfPath,
    mimeType: 'application/pdf',
    title: 'Pdf test'
    };

Platform.OS === ‘ios’ ?
Share.open(shareOptions) : this.sharePDFWithAndroid(pdfPath, ‘application/pdf’); }

My share function for android is :

sharePDFWithAndroid(pdfPath, type) { const { fs, fetch, wrap } = RNFetchBlob; RNFetchBlob.fs.readFile(pdfPath, ‘base64’).then( base64Data => { base64Data = data:${type};base64, + base64Data; Share.open({ url: base64Data }); }); }

After a lot of research, I found out that only base64 data for pdf file can be shared using react-native-share. Is not possible to share using url with “file://…pdfPath…”.

I hope that someone will find this helpfull.

This is also related to #520

After few days stuck in this problem, I figured out that files in cache folder are able to be shared without any problem. But I still waiting a solution to share files from ‘files’ folder.

@ramonscampos please let me know what file types you need to share? I can try to share it as well.

hello。I tried to share the.xls file, but it didn’t work。

import { writeFile, readFile, unlink, DocumentDirectoryPath, ExternalDirectoryPath, } from ‘react-native-fs’

writeFile(file, output(wbout), ‘base64’) .then(res => { console.log(file) this.readFile(filePath) // resolve(file) })

readFile(filePath, “base64”).then((res)=>{ console.log(res) const dataUri = data:excel/xls;base64,${res}; Share.open({ url: dataUri, }); I finally solved the problem of android sharing excel files.Here is the full code。

import XLSX from 'xlsx'
import {
    writeFile,
    readFile,
    unlink,
    DocumentDirectoryPath,
    ExternalDirectoryPath,
} from 'react-native-fs'
import { Platform } from 'react-native'
import { Toast } from '@ant-design/react-native'
import Share from 'react-native-share';


/**
 * excel
 * @param data
 * @param fileName
 */
const exportExcel = (data: Array, fileName: string) => {
    let ipt = new Promise(function(resolve, reject) {
        try {
            const output = str => str
            const path = Platform.select({
                ios: `${DocumentDirectoryPath}/`,
                android: `${ExternalDirectoryPath}/`
            })
            const fileType = 'xls'
            if (data && data.length > 0) {
                let ws = XLSX.utils.aoa_to_sheet(data)
                let wb = XLSX.utils.book_new()
                XLSX.utils.book_append_sheet(wb, ws, fileName)
                const wbout = XLSX.write(wb, {
                    type: 'base64',
                    bookType: fileType
                })
                const file = `${path}${fileName}.${fileType}`
                writeFile(file, output(wbout), 'base64')
                    .then(res => {
                        console.log(file)
                        wxShareFile(fileName, file, resolve, reject)
                        // resolve(file)
                    })
                    .catch(err => {
                        Toast.fail(err)
                        console.log(err)
                        reject(err)
                    })
            } else {
                alert('没有可分享的数据')
            }
        } catch (e) {
            console.log(e)
            reject(e)
        }
    })

    return ipt
}

const wxShareFile = async (fileName, filePath, resolve, reject) => {
    readFile(filePath,"base64").then((res)=>{
       console.log(res)
//You can search for MIME Types using Google to find the corresponding Types of files
// something like it : .xls->application/vnd.ms-excel .Xml-> text/ xml .zip-> applica
        const dataUri = `data:application/vnd.ms-excel;base64,${res}`;
         Share.open({
            url: dataUri,
        });
         // unlink(path);

    })

}



export { exportExcel }