11

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:

4 Answers4

14

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

  • 4
    For me this produces the following 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 }]` -- any ideas what might cause this? – Joel Peltonen Jun 24 '20 at 12:12
  • not working . Getting No Activity found to handle Intent – Krutika Chotara Aug 31 '21 at 06:17
  • 3
    is it working on iOS? – Ante Ereš Dec 17 '21 at 16:12
5

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);
}
kyun
  • 9,710
  • 9
  • 31
  • 66
lZeno
  • 51
  • 1
  • 3
1
import * as FileSystem from 'expo-file-system';
import * as IntentLauncher from 'expo-intent-launcher';
  
 const uri = FileSystem.documentDirectory  + 'Example'.xlsx;

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

Note: get your filelocation on uri

teenage vampire
  • 339
  • 1
  • 5
  • 13
1

For android the IntentLauncher approach will work great. For ios, it is recommended to have another approach:

Install the expo dependencies:

npx expo install expo-sharing

The the code may be like that:

import * as FileSystem from 'expo-file-system';
import * as IntentLauncher from 'expo-intent-launcher';
import { Platform } from 'react-native';
import * as Sharing from 'expo-sharing';

FileSystem.getContentUriAsync(uri).then(cUri => {
    if (Platform.OS === 'ios') {
        Sharing.shareAsync(cUri.uri);
    } else {
        IntentLauncher.startActivityAsync('android.intent.action.VIEW', {
            data: cUri.uri,
            flags: 1,
            type: 'application/pdf',
        });
    }
});
Augusto Vicente
  • 11
  • 1
  • 17
  • 91