You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo 安卓端分享容器组件截图报错如何解决

报错解决方案

你遇到的[TypeError: undefined is not an object (evaluating '_expoSharing.default.shareAsync')]报错核心是expo-sharing模块的导入/安装异常,按以下步骤排查即可:

  • 第一步:安装匹配版本的expo-sharing包
    运行命令:npx expo install expo-sharing,不要直接使用npm/yarn安装,避免出现SDK版本不兼容问题
  • 第二步:修正导入语句
    不要使用默认导入语法,expo-sharing没有默认导出,正确导入方式为:
    import * as Sharing from 'expo-sharing';
    
    如果你之前写的是import Sharing from 'expo-sharing',修改后即可解决90%的同类报错
  • 第三步:优化分享逻辑适配安卓特性
    调用分享前先校验分享功能可用性,同时明确指定图片格式配置,修改后的完整代码如下:
    const shareImageHandler = async () => {
      try {
        // 校验当前设备分享功能是否可用
        const isSharingAvailable = await Sharing.isAvailableAsync();
        if (!isSharingAvailable) {
          console.log("当前设备不支持分享功能");
          return;
        }
        const uri = await captureRef(containerRef, {
          quality: 1,
          format: "jpg",
        });
        // 安卓端明确指定mimeType,避免部分系统识别异常
        await Sharing.shareAsync(uri, {
          mimeType: 'image/jpeg',
          dialogTitle: '分享图片'
        });
        console.log("action succeed");
      } catch (error) {
        console.log("something went wrong!", error);
      }
    };
    
  • 第四步:清理缓存重启项目
    如果修改后仍然报错,运行命令npx expo start --clear清理项目缓存后重新运行即可。

内容的提问来源于stack exchange,提问作者MOHD SHAHZAIB

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 02:54:05