我正在尝试使用react native和expo开发一个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)
}
我在选择文件后读取文件时遇到了一些问题,所以我决定检查 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/
关于可能是什么问题的任何想法