bug: Share API throws an exception while trying to share a local file
See original GitHub issueBug Report
Capacitor Version
Latest Dependencies:
@capacitor/cli: 2.4.2
@capacitor/core: 2.4.2
@capacitor/android: 2.4.2
@capacitor/electron: 2.4.2
@capacitor/ios: 2.4.2
Installed Dependencies:
@capacitor/ios not installed
@capacitor/core 2.4.1
@capacitor/cli 2.4.1
@capacitor/android 2.4.1
@capacitor/electron not installed
Platform(s)
Android
Current Behavior
When you use the .share method to share a file of the app it throws the following error:
D/Capacitor: App paused E/DatabaseUtils: Writing exception to parcel java.lang.SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content://com.***.fileprovider/my_cache_images/log/log_2020-10-01_135743.txt from pid=30905, uid=1000 requires the provider be exported, or grantUriPermission() at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:729) at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:602) at android.content.ContentProvider$Transport.query(ContentProvider.java:231) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:104) at android.os.Binder.execTransactInternal(Binder.java:1021) at android.os.Binder.execTransact(Binder.java:994)
Expected Behavior
Not throwing any error while trying to share a file. And being able to attach it to another apps like Gmail, Whatsapp, etc.
Code Reproduction
Having a file saved for example in cache folder using capacitor FileSystem plugin, try to share it using the Share plugin like:
let shareRet = await Share.share({ title: 'share-title', url: "file:///data/user/0/com.***/cache/log/log_2020-10-01_135743.txt", dialogTitle: 'share-dialog-title' });
Other Technical Details
npm --version
output: 6.14.8
node --version
output: v12.18.3
Additional Context
I am sure that this is because the plugin needs to add Intent.FLAG_GRANT_READ_URI_PERMISSION to the Chooser intent. See this example in stackoverflow: https://stackoverflow.com/a/59439316
I managed to solve it locally using the solution provided in that link and a few others that are pretty much the same. I cannot share it as pull request because I am not using GIT and I don’t have the time at all.
It basically consist in going to the Share.java file and setting the “fileUrl” as local variable variable just before the EXTRA_STREAM is added. Then, after the chooser Intent is created, you “grantUriPermission” to the packages by “queryIntentActivities” in the chooser, just like the example provided in stackoverflow.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:11 (5 by maintainers)
Top GitHub Comments
2.4.3 is out with this fix.
Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Capacitor, please create a new issue and ensure the template is fully filled out.