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

bug: Share API throws an exception while trying to share a local file #3638

Closed
angel-garcia-lopez opened this issue Oct 2, 2020 · 11 comments

Comments

@angel-garcia-lopez
Copy link

angel-garcia-lopez commented Oct 2, 2020

Bug 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.

@jcesarmobile
Copy link
Member

The plugin uses a FileProvider that grants access to urls as long as the file_paths.xml has an entry for where the file is, by default it just includes

<external-path name="my_images" path="." />
<cache-path name="my_cache_images" path="." />

Since you say you are writing and sharing from caches folder that should be enough, can you provide a sample project?

@jcesarmobile jcesarmobile added the needs reply needs reply from the user label Oct 2, 2020
@angel-garcia-lopez
Copy link
Author

I've never done it before and took me some time but here you are... checkout the repo I just created:
https://github.com/angel-garcia-lopez/bug-sample-share

@Ionitron Ionitron removed the needs reply needs reply from the user label Oct 2, 2020
@edwinofsakh
Copy link

#3643 I did a pull request according to the suggested changes.

@jcesarmobile
Copy link
Member

What's the Android version of the device where you tried and failed?
I've tested your sample app on Android 8.1 and Android 9 devices and share worked.

@angel-garcia-lopez
Copy link
Author

angel-garcia-lopez commented Oct 6, 2020 via email

@edwinofsakh
Copy link

I have a similar "Permission Denial" issue on the emulator with Android 10.

@jcesarmobile
Copy link
Member

I've been able to reproduce the error message on Android 10 devices. The error don't prevent Share from working, but I've sent a PR that prevents the error from appearing. #3655

As I said, the file provider already grants the read permission to the content URIs by default, not sure why it happens on Android 10, but the problem is on the Chooser intent that wraps the send intent, probably when tries to preview the attachment (since it's an Android 10+ feature). Using the send intent without the chooser don't present the error, but loses some functionality like copying the file or direct share.

The PR is simpler than the other proposed changes, and also the other proposed changes are incomplete, if the URI permission is granted that way, it has to be revoked after the share is done and that wasn't implemented. So would be good if you could try my changes and verify if it works for you.

@angel-garcia-lopez
Copy link
Author

angel-garcia-lopez commented Oct 9, 2020 via email

@jcesarmobile
Copy link
Member

closing since I merged #3655

@imhoffd
Copy link
Contributor

imhoffd commented Nov 18, 2020

2.4.3 is out with this fix.

@ionitron-bot
Copy link

ionitron-bot bot commented Nov 11, 2022

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.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Nov 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants