Reputation: 21
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>
Upvotes: 2
Views: 1139