-
Notifications
You must be signed in to change notification settings - Fork 4
Description
I have a workaround for this, but there are a few bits that I don't know how to do yet before submitting a PR.
Questions before a PR
Does anyone know how to put AndroidManifest.xml changes or other res directory changes into a NativeScript plugin?
Summary of problem
Sharing a PDF file to an email seemed to work okay, but sharing it to Adobe Reader or a print service failed. I think the behavior was also different between Android 7 and 10, though I haven't verified that recently.
Solution
Here's code that seems to be working. I think this could work in share-file.android.js with a few tweaks. Notably:
- I'm expecting a
file://url only. I think there is existing code to massage file paths and whatnot that should be utilized instead. - I hard-coded
application/pdf. The existing code calculates a MIME type, and then ignores it. It should probably be used. - The FileProvider needs a "URI authority". Perhaps this can be automatically based off of the app?
- I took out the configurable intent text.
- The FileProvider requires changes to
AndroidManifest.xmland a new file calledfile_paths.xml. I don't know how to properly include these changes in a NativeScript plugin. Or do they have to be in the install instructions somehow?
TypeScript
const newFile = new java.io.File(new java.net.URI(url));
// the "com.iconcmo.fileprovider" has to match what is in the androidx.core.content.FileProvider section of AndroidManifest.xml
const fileProviderFile = androidx.core.content.FileProvider.getUriForFile(
Application.android.context,
"com.iconcmo.fileprovider",
newFile
);
const intent = new android.content.Intent(
android.content.Intent.ACTION_SEND
);
intent.setType("application/pdf");
intent.addFlags(
android.content.Intent.FLAG_GRANT_READ_URI_PERMISSION |
android.content.Intent.FLAG_GRANT_WRITE_URI_PERMISSION
);
intent.putExtra(android.content.Intent.EXTRA_STREAM, fileProviderFile);
Application.android.foregroundActivity.startActivity(
android.content.Intent.createChooser(intent, "Open file:")
);AndroidManifest.xml (inside <application> tags)
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.iconcmo.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="reports" path="/"/>
</paths>Resources
- https://developer.android.com/reference/androidx/core/content/FileProvider
- https://github.com/react-native-share/react-native-share/
All of that said, I completely get that this may not be an appropriate thing for this library — feel free to close this issue if so! I also unfortunately only know enough about sharing things on Android to have made the fix above work for the singular purpose of sharing PDFs from my app, but nothing beyond that!