在从expo的DocumentPicker获取路径之后,我目前正在尝试使用expo的FileSystem打开文件,但是我找不到找到使DocumentPicker的URI可读到FileSystem的方法。一切正常,我在权限或其他方面都没有问题,但是到目前为止,我能找到的每个教程基本上都是针对使用公开的本机代码或Java响应本机项目的。
在这里,我称之为DocumentPicker:
import * as DocumentPicker from 'expo-document-picker';
const Hello = ({navigation}) => (
<HelloView>
<HelloButton
style={{
backgroundColor: WhatsappStyles.colors.lightGreen,
}}
activeOpacity={0.5}
onPress={() => {
DocumentPicker.getDocumentAsync({
copyToCacheDirectory: false,
}).then(({uri}) => {
navigation.navigate('LoadFile', {path: uri});
});
}
}>
<HelloButtonText>Go to App</HelloButtonText>
</HelloButton>
</HelloView>
);
在这里,我尝试(未成功)阅读路径:
import * as FileSystem from 'expo-file-system';
const LoadFile = ({navigation, route}) => {
let [loading, setLoading] = useState(true);
useEffect(() => {
FileSystem.readAsStringAsync(route.params.path).then(result => {
// do something here
}
setLoading(false);
}, []);
...
我正在使用react-navigation / stack进行堆栈导航,并使用styled-components / native来构建组件。
因此,据我所知,没有办法将content:// uri“转换”为file:// uri,但实际上您可以使用FileSystem的downloadAsync函数将文件下载到缓存或内部存储中,并使用文件:// uri从那里产生。
例如
DocumentPicker.getDocumentAsync({
copyToCacheDirectory: false,
}).then(({contentUri}) => {
FileSystem.downloadAsync(
contentUri,
FileSystem.documentDirectory + '<file name>')
.then(({uri}) => {
FileSystem.readAsStringAsync(uri)
.then(result => {
// do what you want here.
});
});
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句