打开.pdf,.docx,.xlsx等,并在React Native中使用默认设备应用程序



我正在尝试使用React Native打开.pdf,.docx,.xlsx和其他类似文件。

我目前正在将文件从Amazon S3存储桶下载到设备,然后尝试使用React-Native-Fetch-Blob android.actionViewIntent(res.path(), mimeType)在设备上打开它们。

这可以与图像一起使用,但是在其他任何内容中,我都会收到以下错误:

Error: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///storage/emulated/0/Android/data/myappname/files/1475.pdf typ=application/pdf flg=0x10000000 }

编辑:对于任何想查看最终起作用的实现的人(使用React-Native-File-System和React-Native-Fetch-Blob):

):
const fileType = fileDownloadPath.split('.').pop();
const SavePath = Platform.OS === 'ios' ? RNFS.DocumentDirectoryPath : RNFS.ExternalDirectoryPath;
const mimeType = getMimeType(fileType); // getMimeType is a method (switch statement) that returns the correct mimetype
const path = `${SavePath}/${filename}.${fileType}`;
const android = RNFetchBlob.android;
RNFS.downloadFile({
  fromUrl: url,
  toFile: path,
}).promise.then(() => {
  android.actionViewIntent(path, mimeType)
    .then((success) => {
      console.log('success: ', success)
    })
    .catch((err) => {
      console.log('err:', err)
    })
});

显然,您的设备没有安装其他文件查看器应用程序,例如Polaris Office或Office Suite或任何其他类似的文件查看器。

通常,这是您在这种问题上得到的例外。您可以显示警告/吐司或显示一些链接以从Play商店下载这些应用程序。

我有这个,这是一种解决它的方法。

  try{
    startActivity(intent);
  } catch (ActivityNotFoundException e){
    Toast.makeText(MainActivity.this,
            "Error showing pdf", Toast.LENGTH_LONG).show();
  }

最新更新