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

使用Expo开发React Native时,DocumentPicker报'pick'为null错误求助

解决Expo中DocumentPicker出现Cannot read property 'pick' of null的问题

问题根源

在Expo项目中,不能直接使用第三方库react-native-document-picker,因为Expo有自己官方维护的文档选择器expo-document-picker,两者API不兼容,直接导入前者会导致模块加载失败,出现DocumentPicker为null的错误。

解决步骤

1. 移除旧库

先卸载原来的react-native-document-picker:

npm uninstall react-native-document-picker
# 或使用yarn
yarn remove react-native-document-picker

2. 安装Expo官方文档选择器

安装expo-document-picker:

npx expo install expo-document-picker

3. 修改导入与代码逻辑

替换原有的导入语句,并适配Expo的API:

导入语句修改为:

import * as DocumentPicker from 'expo-document-picker';

调整文件选择函数:

const openFilePicker = async () => {
  try {
    const result = await DocumentPicker.getDocumentAsync({
      type: 'application/pdf', // 指定PDF的MIME类型
      copyToCacheDirectory: true, // 可选,将文件复制到缓存目录,便于后续操作
    });

    if (result.type === 'success') {
      // 打印选中文件的信息
      console.log(
        '文件URI:', result.uri,
        'MIME类型:', result.mimeType,
        '文件名:', result.name,
        '文件大小:', result.size
      );
    } else {
      // 用户取消了选择操作
      console.log('用户取消文件选择');
    }
  } catch (err) {
    console.error('文件选择出错:', err);
  }
};

4. 按钮代码保持不变

原有的按钮触发逻辑无需修改:

<Button
  title="Select PDF"
  color="green"
  onPress={openFilePicker}
/>

补充说明

  • Expo Go中无需额外配置权限,安装后即可直接测试;
  • 如果后续需要将文件上传到服务器,可以使用result.uri作为文件路径,配合expo-file-system或其他网络库实现上传。

内容的提问来源于stack exchange,提问作者Ons Jannet

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 07:53:11