Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View or open PDF files stored locally Expo React Native

Is there a solution to view or open PDF Files using Expo (without Expo eject)? It's not necessary to open file inside App, it's can be a local filemanager.

What i tried:

  • Linking does not open locally files, only online sources
  • React Native PDF doesn't work with expo
  • Expo Media Library allows to download files, but not open it.
like image 721
n8872 Avatar asked Jan 19 '20 18:01

n8872


People also ask

Where does expo save files?

documentDirectory + 'myDirectory/myFile' . Expo APIs that create files generally operate within these directories.

Is Expo THE BEST FOR React Native?

If you are given a project that needs rapid development, and you have picked React Native to build the cross-platform app, Expo is the best fit for you. With Expo, you can build and deploy React Native apps for both iOS and Android with ease. With Expo, you will never touch any native iOS or native Android code.


2 Answers

My solution:

Use FileSystem.getContentUriAsync() and Expo IntentLauncher

import * as FileSystem from 'expo-file-system';
import * as IntentLauncher from 'expo-intent-launcher';

FileSystem.getContentUriAsync(uri).then(cUri => {
  IntentLauncher.startActivityAsync('android.intent.action.VIEW', {
      data: cUri.uri,
      flags: 1,
      type: 'application/pdf'
   });
});

enter image description here

like image 188
n8872 Avatar answered Sep 20 '22 15:09

n8872


I tried this solution https://stackoverflow.com/a/59813652/16799160 and I get this error:

[Unhandled promise rejection: Error: Encountered an exception while calling native method: Exception occurred while executing exported method startActivity on module ExpoIntentLauncher: No Activity found to handle Intent { act=android.intent.action.VIEW typ=application/pdf flg=0x1 }]

After that, I modified data: cUri.uri to data: cUri based on expo doc and works fine.
Remember, this is an android-only solution

import * as FileSystem from 'expo-file-system';
import * as IntentLauncher from 'expo-intent-launcher';

try {

  const cUri = await FileSystem.getContentUriAsync(uri);
             
  await IntentLauncher.startActivityAsync("android.intent.action.VIEW", {
      data: cUri,
      flags: 1,
      type: "application/pdf",
  });
}catch(e){
    console.log(e.message);
}
like image 21
lZeno Avatar answered Sep 21 '22 15:09

lZeno