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:
documentDirectory + 'myDirectory/myFile' . Expo APIs that create files generally operate within these directories.
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.
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'
});
});
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With