Open file thats been downloaded to the device

I have a pdf thats been downloaded and saved to the file system using FileSystem.writeAsStringAsync.
I would like to simply link to it and open it on the phone but have been unable to find a way to do so.

React Native’s Linking.openURL has not worked and Expo’s WebBrowser.openBrowserAsync crashes Expo entirely when I pass a file:// url to it.

Is there a solution to this? It seems to be a pretty simple use case. The use case for this is when the device has no internet so an online solution is not possible.

Failing this, is there a way to open or link to a pdf thats been bundled in the app?

Thanks very much.

1 Like

Hi @gooddog - I believe that Linking.openURL should work for this case. Which platform are you seeing this on, and to which directory did you download the PDF?

If you could create a snack that minimally reproduces this issue, that would be super helpful in tracking this down.

Thanks for the reply.

The error I’m receiving when attempting to use openURL is

Could not open URL file:///data/user/0/host.expo.exponent/files/ExperienceData/%2540gooddog%252itransfuse/iTransfuseAppCache/BP/RC/rc.pdf: file:///data/user/0/host.expo.exponent/files/ExperienceData/%2540gooddog%252itransfuse/iTransfuseAppCache/BP/RC/rc.pdf exposed beyond app through Intent.getData() 

The app is fairly complicated and would take some time to extricate into a simplified snack. I know this doesn’t help much with finding the issue, but perhaps you could give me some pointers based on the overall workflow of the app.

The app pretty much does the following.

  1. The first time it is loaded, the app creates a number of folders and downloads a bunch of json files and pdfs into FileSystem.documentDirectory + 'iTransfuseAppCache/BP'. These files are then updated every day when the app is opened (assuming theres an internet connection), essentially caching them.
  2. When a specific screen is opened, the app uses NetInfo.isConnected to check connection status and either collects data from the API if the device has internet, or collects it from the json files downloaded earlier.
  3. On this page is a “View PDF” button. If the device has internet, when it is pressed, it loads the URL to a PDF (obtained from the API) into a WebView. If the devise does NOT have internet, it uses Linking.openURL to link to one the pdf that was downloaded. The URL for this cached file is something like file:///data/user/0/host.expo.exponent/files/ExperienceData/%2540gooddog%252itransfuse/iTransfuseAppCache/BP/RC/rc.pdf

Is this possible? Or are files downloaded to the FileSystem.documentDirectory folder completely inaccessible outside the app. If this is the case, is there a way to download these files to more accessible locations.

Thanks very much

hi @gooddog, opening pdfs has been a running issue with React Native and android for a while. There is a hacky workaround where people pass the url with gdoc preview:
https://github.com/facebook/react-native/issues/6488

Hi @quinlanj, thanks, we are aware of this work around and are using it for when we wish to display an online pdf.

This question is more about how to link directly to a PDF that has been downloaded to the file system and have it open in whatever pdf app the device has installed so it can be viewed offline.

3 Likes

Hi,

I’m working on a solution to have a PDF offline viewer (android capable, using expo):

https://github.com/xcarpentier/rn-pdf-reader-js

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.