2

I want to open files from my Android app (Cordova) in external apps with write access, so they can make changes. (Example: sign a PDF).

The files to open are under external-files-path and can be opened. Unfortunately, the changes are not saved.

Since Android 11 (API Level 30) the file:// calls don't work anymore (with disableDeathOnFileUriExposure hack). With the content:// call I get no write access passed to the app.

I use the following logic to open the file (the coding is from https://github.com/pwlin/cordova-plugin-file-opener2):

File file = new File(fileName);
Intent intent = new Intent(Intent.ACTION_VIEW);
Context context = cordova.getActivity().getApplicationContext();
Uri path = FileProvider.getUriForFile(context, cordova.getActivity().getPackageName() + ".fileOpener2.provider", file);
intent.setDataAndType(path, contentType);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (openWithDefault) {
    cordova.getActivity().startActivity(intent);
} else {
    cordova.getActivity().startActivity(Intent.createChooser(intent, "Open File in..."));
}

AndroidManifest.json

<provider android:authorities="${applicationId}.fileOpener2.provider" android:exported="false" android:grantUriPermissions="true" android:name="io.github.pwlin.cordova.plugins.fileopener2.FileProvider">
    <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/opener_paths" />
</provider>

opener_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="files" path="." />
    <cache-path name="cache" path="." />
    <external-files-path name="external-files" path="." />
    <external-cache-path name="external-cache" path="." />
    <external-path name="external" path="." />
</paths>
ToMoAxi
  • 21
  • 2
  • Your code looks ok. Blame that external app. Which app did you use? Use another app. – blackapps Aug 17 '21 at 14:44
  • "With the content:// call I get no write access passed to the app" -- how precisely have you determined this? There is no requirement for a PDF viewer to implement edit capability, and there is no requirement for a PDF editor to save the content back to the original location. *Ideally*, the editor would offer that, but it is up to the developers of the editor, not you or I. – CommonsWare Aug 17 '21 at 14:56
  • @blackapps I use Adobe Acrobat Reader and MS Excel. Both worked fine with the file:// calls. Is there any way I can check if the external apps have implemented this correctly? Maybe via logcat? – ToMoAxi Aug 18 '21 at 07:38
  • Read this as you are not alone: https://stackoverflow.com/questions/68819154/how-to-open-pdf-on-android-using-intent-to-edit-file?noredirect=1#comment121636419_68819154 – blackapps Aug 18 '21 at 07:43

0 Answers0