查看或打开存储在本地 Expo React Native 的 PDF 文件

n88*_*872 7 reactjs react-native expo

是否有使用 Expo(没有 Expo 弹出)查看或打开 PDF 文件的解决方案?不需要在 App 内打开文件,它可以是本地文件管理器。

我试过的:

n88*_*872 11

我的解决方案:

使用FileSystem.getContentUriAsync()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'
   });
});

Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 对我来说,这会产生以下错误:`[未处理的承诺拒绝:错误:调用本机方法时遇到异常:在模块 ExpoIntentLauncher 上执行导出方法 startActivity 时发生异常:找不到处理 Intent 的活动 { act=android.intent.action.查看typ=application/pdf flg=0x1 }]` - 有什么想法可能会导致这种情况吗? (4认同)
  • 它可以在 iOS 上运行吗? (4认同)

小智 5

我尝试了这个解决方案/sf/answers/4186955671/并收到此错误:

[未处理的承诺拒绝:错误:调用本机方法时遇到异常:在模块 ExpoIntentLauncher 上执行导出方法 startActivity 时发生异常:找不到处理 Intent { act=android.intent.action.VIEW Typ=application/pdf flg=0x1 的活动}]

之后,我根据expo doc将 data: cUri.uri 修改为 data: cUri并且工作正常。
请记住,这是仅限 Android 的解决方案

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);
}
Run Code Online (Sandbox Code Playgroud)