gpt4 book ai didi

expo - Android 上 FileSystem.getInfoAsync 或 FileSystem.readAsStringAsync 的错误 : 'Location...not readable' when using localUri from Asset. loadAsync

转载 作者:行者123 更新时间:2023-12-05 06:07:14 28 4
gpt4 key购买 nike

尝试使用 Asset.loadAsync.txt Assets 作为字符串加载到 Expo 中

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

但是,FileSystem.readAsStringAsync(localUri) 会抛出一个错误:Location 'file:/data/user/0/host.exp.exponent/files/.expo-internal/deaba7963cc3ce7f03f36c3b210a771f .txt' 不可读

这只发生在 ANDROID 上。文件在 iOS 上加载正常。

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

世博会项目示例 Published Here

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

所以我所做的就是在编码函数中传递 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,...}

关于expo - Android 上 FileSystem.getInfoAsync 或 FileSystem.readAsStringAsync 的错误 : 'Location...not readable' when using localUri from Asset. loadAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65537705/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com