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)
小智 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)
| 归档时间: |
|
| 查看次数: |
3576 次 |
| 最近记录: |