错误:在 Android 上使用 Asset.loadAsync for FileSystem.getInfoAsync 或 FileSystem.readAsStringAsync 时'Locat



尝试使用Asset.loadAsync在Expo中将.txt资产加载为字符串

Asset.loadAsync(module)解析并提供localUri

然而,FileSystem.readAsStringAsync(localUri)抛出一个错误:Location 'file:/data/user/0/host.exp.exponent/files/.expo-internal/deaba7963cc3ce7f03f36c3b210a771f.txt' isn't readable

这只发生在安德鲁身上文件在iOS上加载良好。

预期行为:Asset.loadAsync(module)提供的文件可读。

在此发布的世博项目示例

App.js

import * as React from 'react';
import { Text, View, StyleSheet } from 'react-native';
import Constants from 'expo-constants';
import * as FileSystem from 'expo-file-system'
import { Asset } from 'expo-asset'
export default function App() {
const [message, setMessage] = React.useState("")
React.useEffect(() => {
(async () => {
const [{localUri}] = await Asset.loadAsync(require('./assets/fff.txt'));
try {
const str = await FileSystem.readAsStringAsync(localUri);
setMessage(str);
} catch (e) {
setMessage(e.message);
}
})()
}, [])
return (
<View style={styles.container}>
<Text>Message: { message }</Text>
</View>
);
}
const styles = ...

assets/fff.txt

This text is from fff.txt
Got it successfully!

metro.config.js

const { getDefaultConfig } = require('@expo/metro-config');
const defaultConfig = getDefaultConfig(__dirname);
module.exports = {
resolver: {
assetExts: [...defaultConfig.resolver.assetExts, 'txt'],
},
};

软件包.json

{
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web",
"eject": "expo eject"
},
"dependencies": {
"@expo/metro-config": "^0.1.48",
"expo": "~40.0.0",
"expo-asset": "~8.2.1",
"expo-constants": "~9.3.1",
"expo-file-system": "~9.3.0",
"expo-status-bar": "~1.0.3",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-40.0.1.tar.gz",
"react-native-paper": "3.6.0",
"react-native-web": "~0.13.12"
},
"devDependencies": {
"@babel/core": "~7.9.0"
},
"private": true
}

App.json

{
"expo": {
"name": "healthy-edamame",
"slug": "snack-9f640e40-68ed-4cdc-92a6-37315a56d405",
"version": "1.0.0",
"orientation": "portrait",
"icon": "./assets/icon.png",
"splash": {
"image": "./assets/splash.png",
"resizeMode": "contain",
"backgroundColor": "#ffffff"
},
"updates": {
"fallbackToCacheTimeout": 0
},
"assetBundlePatterns": [
"**/*"
],
"ios": {
"supportsTablet": true
},
"android": {
"adaptiveIcon": {
"foregroundImage": "./assets/adaptive-icon.png",
"backgroundColor": "#FFFFFF"
}
},
"web": {
"favicon": "./assets/favicon.png"
}
}
}

我得到了同样的错误,当我控制台记录localUri时,我得到了低于输出的结果

/data/user/0/host.exp.exponent/cache/ExperienceData/72a2e181-7f07-480e-bf63-ddb4114958a6.jpeg

所以我所做的只是在encode函数中传递localUri,并使用返回的url,如下所示

const encode = (uri) => {
if (Platform.OS === 'android') return encodeURI(`file://${uri}`)
else return uri
}
let newurl = encode(localUri);

您也可以使用下面的代码来检查uri路径是否存在

const fileInfo = await FileSystem.getInfoAsync(newurl);
console.log(fileInfo) //Object{"exists": true,...}

最新更新