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

Does open with url: "file://<file_path>" work on Android? #629

Closed
tststs opened this issue Nov 14, 2019 · 24 comments
Closed

Does open with url: "file://<file_path>" work on Android? #629

tststs opened this issue Nov 14, 2019 · 24 comments
Labels
help wanted os: android question stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@tststs
Copy link

tststs commented Nov 14, 2019

I try to share a PDF file from the file system but it just doesn't work.

  • i double checked if the file exists
  • i asked for permission to read storage and it is allowed by the user
  • when sharing it e.g. with google drive it always says that there is "no data" to share
  • i render the PDF before sharing it so i also know that the file is not empty or corrupt

Also the docs are a bit unclear about if it's even possible to do this.
As in the docs for url: URL you want to share (only support base64 string in iOS & Android ).

Does this mean only sharing a string is possible like: url: "data:<data_type>/<file_extension>;base64,<base64_data>" is possible? because scrolling down a bit it is stated that you can directly pass a file by usingfile://...

My options:

const options = {"filename": "test.pdf", "url": "file:///data/user/0/com.x.x/files/6cfe208c-1b45-4fac-af0f-aadb7e678a6b.pdf"}

Edit:
There are no errors. It resolves with success

Thanks in advance!

@jgcmarins
Copy link
Member

Android share with file path should work.
Possibly we're facing a bug since there are few issues reporting this behaviour.
Thanks for all information you've provided.
Are you testing this with real device or emulator?

@tststs
Copy link
Author

tststs commented Nov 14, 2019

hey!

testing on a real device:

  • galaxy s9
  • Android 9

if you need any other information don't hesitate to ask. For now i'm using the fallback by reading the file with rn-fetch-blob then share it via base64 string.

@jgcmarins
Copy link
Member

@tststs great, thanks for your reply.
I don't know if I will be able to dig into this problem.
Feel free to share more information or bring up a solution.
I'll be glad to help with code review.

@kaanduraa
Copy link

any updates on this ?

@jgcmarins
Copy link
Member

As far as I remember, sharing using file path is working fine.
You can test it with our example.

@Alvee9
Copy link

Alvee9 commented Nov 22, 2019

As far as I remember, sharing using file path is working fine.
You can test it with our example.

The example given there doesn't use file path, rather it uses data URLs with base64 encoded strings which doesn't have any issue. The only problem in this scenario is that url with path like "file://<file_path>" doesn't work. (NB: It works fine on ios)

@anastely
Copy link

@jgcmarins hey, I faced something same,
when URL be like this "/storage/emulated/0/Downloads/Bill765.pdf" it does not work so I can't share this PDF

But when URL be like this "/data/user/0/com.lmaalam_admin/cache/Bill197994929333884192053.pdf"
it's working fine!

So I'm forced to use the first file URL for other functionality and want to share it in this package, any idea about this?

@olessavluk
Copy link

Hi 👋

I also have this issue, when sharing file using local path - other applications do not receive it.
Logcat show NULL (clip={text/plain NULL}) instead of file:

03-23 00:34:02.875  2548  3704 I ActivityManager: START u0 {act=android.intent.action.SEND_MULTIPLE typ=text/plain flg=0xb080001 cmp=org.telegram.messenger/org.telegram.ui.LaunchActivity clip={text/plain NULL} (has extras)} from uid 10192 on display 0
03-23 00:34:02.875  2548  3704 I ActivityManager: Ignoring FLAG_ACTIVITY_NEW_DOCUMENT, launchMode is "singleInstance" or "singleTask"

I have created reproducible repo - https://github.com/olessavluk/sharebug

  1. run it
  2. click button "Share text.txt"

@jgcmarins any chance you could take a look?

@nirre7
Copy link
Contributor

nirre7 commented Mar 28, 2020

I've got the same issue with Samsung galaxy s9+, android 10.
Works with a pixel simulator though. Very weird...
I.e urls that looks like: file:///storage/6664-3666/DCIM/Camera/20200328_152552.jpg doesn't work.
Does not work with the url or urls options.

@enesozturk
Copy link

I have the same issue. Is there anybody solved the issue? I have text file and trying to share it with path like file:///data/user/0/com.myapp/files/my_file.txt but not working in (just) android

@taduyde
Copy link

taduyde commented Apr 2, 2020

I met the same issue on Android. we can't share with local file ""/data/user/0/com.lmaalam_admin/cache/Bill197994929333884192053.pdf"", we have to convert it to base 64.

@enesozturk
Copy link

I met the same issue on Android. we can't share with local file ""/data/user/0/com.lmaalam_admin/cache/Bill197994929333884192053.pdf"", we have to convert it to base 64.

Thanks for the idea. I will check and share the result soon.

@nirre7
Copy link
Contributor

nirre7 commented Apr 7, 2020

Hi again.
I created a pull request that should enable sharing of files on secondary storage, i.e sd cards: #754

@taduyde
Copy link

taduyde commented Apr 8, 2020

Hi again.
I created a pull request that should enable sharing of files on secondary storage, i.e sd cards: #754

Hi @nirre7,
Your pull request is fixed for sharing issue: file://<file_path> on android. is that right?

@MateusAndrade
Copy link
Collaborator

Your pull request is fixed for sharing issue: file://<file_path> on android. is that right?

Hey @taduyde would you mind helping us testing that? 😄

@nirre7
Copy link
Contributor

nirre7 commented Apr 8, 2020

Hi @nirre7,
Your pull request is fixed for sharing issue: file://<file_path> on android. is that right?

It depends on where the file(s) are. If the file is on secondary storage like a sd card then it should work.

For more information https://developer.android.com/reference/androidx/core/content/FileProvider there is different configs on android to enable "access" to different locations.

But like @MateusAndrade said would be great if you could help us test it, it might be that we need to add more configuration to handle different scenarios/usecases.

@taduyde
Copy link

taduyde commented Apr 8, 2020

Hi @nirre7, @MateusAndrade,

I already tested this pull request, but that pull request not help we resolve this issue with local file as here: /data/user/0/com.lmaalam_admin/cache/Bill197994929333884192053.pdf

@nirre7
Copy link
Contributor

nirre7 commented Apr 8, 2020

local file

The PR fixes secondary storage for example sd cards, i.e not "local files".

Im guessing (!) that the config your scenario needs is (which isn't setup in this lib, yet):
<files-path name="someName" path="/" />

https://developer.android.com/reference/androidx/core/content/FileProvider#SpecifyFiles

@MateusAndrade
Copy link
Collaborator

@taduyde probably you need to transform your file on a base64 before sharing that. There are been some problems when passing file whenever you call the open that we need to investigate. 😿

I think this can help you: https://github.com/react-native-community/react-native-share#share-remote-pdf-file-with-gmail--whatsapp-ios

@kaioduarte
Copy link
Collaborator

Just tested the new version with mentioned PR and it's working for local files and files of the same package (/data/user/0/com.myapp), for different package it throws an error: (Permission denied)

Local file:

local-file

Same package:

ScreenRec-20200408-214632

File from different package:

local-file-different-package

@enesozturk
Copy link

I met the same issue on Android. we can't share with local file ""/data/user/0/com.lmaalam_admin/cache/Bill197994929333884192053.pdf"", we have to convert it to base 64.

Thanks for the idea. I will check and share the result soon.

Converting file to base64 before share solved my issue. Thanks!

@coolvasanth
Copy link

I have a local Image that needs to be shared. For ex in My react native project, I have a Image named Resources/1.png I want to share this Image. How can I achieve it?

@nasreekar
Copy link

nasreekar commented May 13, 2020

I met the same issue on Android. we can't share with local file ""/data/user/0/com.lmaalam_admin/cache/Bill197994929333884192053.pdf"", we have to convert it to base 64.

Thanks for the idea. I will check and share the result soon.

Converting file to base64 before share solved my issue. Thanks!

I have tried converting it to base64 using fetch blob but I'm seeing an error saying that the file or directory doesn't exist. Cannot share files on Android only. iOS is working fine.

below is my original code before converting it to base64. This code is sharing correctly with ios but not with android. In android, I see cannot share file when i try to share with email or whatsapp

`shareBill = async (title: string) => {
const billPdfPath = this.props.billPdfDataFetched?.path;

if (!billPdfPath) {
  return;
}

const newPath = `${RNFetchBlob.fs.dirs.CacheDir}/${title}_bill.pdf`;

await RNFetchBlob.fs.cp(billPdfPath, newPath);

const shareOptions = {
  title: title,
  subject: `${strings('bill.emailSubject')} – ${title}`,
  message: `${title} bill`,
  url: `file://${newPath}`,
  type: 'application/pdf',
  failOnCancel: true,
};

try {
  await RNShare.open(shareOptions);
  track(EVENTS.BILL_SHARE);
} catch (err) {
  track(EVENTS.BILL_SHARE, false);
} finally {
  RNFetchBlob.fs.unlink(newPath);
}

};`

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. You may also mark this issue as a "discussion" and i will leave this open

@github-actions github-actions bot added the stale There has been a lack of activity on this issue and it may be closed soon. label May 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted os: android question stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests