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)
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 = {
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.