Skip to content

Problems sharing a PDF file to print service #3

@dahjelle

Description

@dahjelle

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions