如何使用 expo 链接在外部应用程序中有效打开下载的文件

问题描述

我正在从 URI 下载文件,并尝试在另一个应用程序中打开它。此文件一个简单的 .xlsx 电子表格。我正在使用 Android 设备进行测试。下面的代码一个可重现的例子:

import React,{ useCallback } from 'react'
import { Button,View } from 'react-native'
import * as Linking from 'expo-linking'
import * as FileSystem from 'expo-file-system'

const App = () => {
  const download = useCallback(async () => {
    const downloadUri = 'http://url.to/my/spreadsheet.xlsx')
    const localPath = `${FileSystem.cacheDirectory}spreadsheet.xlsx`
    try {
      await FileSystem.downladAsync(downloadUri,localPath)
        .then(async ({ uri }) => {
          const contentURL = await FileSystem.getContentUriAsync(uri)
          await Linking.openURL(contentURL)
        })
        .catch((err) => console.log(err))
    } catch (err) {
      console.log(err)
    }
  },[])

  return (
    <View>
      <Button onPress={download}>
        Pree Me!
      </Button>
    </View>
  )
}

控制台日志中未显示任何错误,但是当 Linking 尝试打开文件时,会出现错误“Google 表格无法打开您的电子表格”。当我尝试打开其他文件类型时会发生同样的事情:

  • .docx 使用 Google 文档
  • .pptx 使用 Google 幻灯片
  • .pdf 使用云端硬盘
  • ...等等

知道是什么原因造成的吗?

注意

我使用以下方法验证了 URL 返回的文件是否未损坏:

curl -X GET http://url.to/my/spreadsheet.xlsx -o spreadsheet.xlsx(从设备内部),可以使用 Google 表格正常打开。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)