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

使用Expo开发EPUB阅读器时FileSystem.getInfoAsync()致应用崩溃

Expo中调用FileSystem.getInfoAsync()导致应用崩溃的问题分析

我正在使用React Native和Expo开发一款EPUB阅读器,在尝试验证文件URI有效性时,调用FileSystem.getInfoAsync()会导致应用在Expo Go中直接崩溃,需要重启服务器重新登录。示例文件的URI为:

file:///var/mobile/Containers/Data/Application/30F628C2-C062-4449-B13B-B4EBE68CFC6F/Library/Caches/ExponentExperienceData/%40omar4tw%2Finkwell/DocumentPicker/E15B6040-2929-41C2-83D6-B0AB4D9E1C34.epub/

相关代码

提示用户选择文件的代码

// method to pick a file from the file system from FileSystemHelper class
static async pickFile() {
    try {
        // prompt the user to pick files
        let result = await DocumentPicker.getDocumentAsync({ multiple: true })
        if (result.type === 'cancel') {
            // The user cancelled the file picker
            return;
        }

        return result
    }
    catch (err) {
        console.log("Error while picking files: ", err)
    }
}

检查文件是否存在的代码

// method used to check if the file exists from the FileSystemHelper class
static async checkFileExists(uri) {
    try {
        // check if the file exists
        const fileExists = await FileSystem.getInfoAsync(uri)
        if (!fileExists.exists) {
            console.log("File does not exist")
            return false
        }
        return true
    }
    catch (err) {
        console.log("Error while checking if file exists: ", err)
    }
}

添加书籍按钮点击事件代码

// function to handle picking a file
const handlePickFile = async () => {
    const result = await FileSystemHelper.pickFile()

    // decode the URI
    result.uri = decodeURI(result.uri)
    const URI = new URL(result.uri)

    // check that the file exists
    const fileExists = await FileSystemHelper.checkFileExists(URI)
    if (!fileExists) {
        console.log("File does not exist")
        return
    }
    
    // parse the result into a book object
    const info = await FileSystemHelper.extractInfo(URI)
}

可能的原因

  • 参数类型错误:FileSystem.getInfoAsync()要求传入字符串类型的URI,但你在handlePickFile中把URI转换成了URL对象,直接传递该对象会导致底层无法正确解析,触发崩溃。
  • URI末尾的斜杠问题:示例URI末尾带有/,系统会将其识别为目录而非文件,FileSystem.getInfoAsync()在处理这类路径时,iOS的Expo Go环境可能存在未捕获的异常,导致应用直接退出。
  • 不必要的URI解码:DocumentPicker返回的URI已经经过正确编码,手动调用decodeURI()会破坏原有格式,导致路径中特殊字符处理异常,进而让getInfoAsync()无法定位文件,引发崩溃。

内容的提问来源于stack exchange,提问作者Omar El Atyqy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 11:55:39