Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems sharing a PDF file to print service #3

Open
dahjelle opened this issue Dec 4, 2020 · 0 comments
Open

Problems sharing a PDF file to print service #3

dahjelle opened this issue Dec 4, 2020 · 0 comments

Comments

@dahjelle
Copy link

dahjelle commented Dec 4, 2020

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.xml and a new file called file_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


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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant