gpt4 book ai didi

reactjs - Expo FileSystem.readAsStringAsync 无法读取文件

转载 作者:行者123 更新时间:2023-12-03 14:07:34 25 4
gpt4 key购买 nike

我有以下设置,当用户使用 ImagePicker 拍照时,它将使用以下代码将其保存到 FileSystem.documentDirectory:

saveAvatar = async (uri) => {
await Expo.FileSystem.moveAsync({
from: uri,
to: Expo.FileSystem.documentDirectory + 'avatar/profile'
})
}

_takePhoto = async () => {
const result = await ImagePicker.launchCameraAsync({
allowsEditing: false,
base64: true,
quality: 0.4
});

if (!result.cancelled) {
this.setState({ image: result.base64 });
this.saveAvatar(result.uri)
}
};

然后我尝试使用以下方法检查检索它:

ensureDirAsync = async () => {
const props = await FileSystem.getInfoAsync(FileSystem.documentDirectory + 'avatar/');

if (props.exists && props.isDirectory) {
return props;
}

try {
await FileSystem.makeDirectoryAsync(FileSystem.documentDirectory + 'avatar/', { intermediates: true });
}
catch (e) {
console.log(e);
}

return await this.ensureDirAsync()
}

getAvatar = async () => {
let dir = await this.ensureDirAsync(),
filename = await FileSystem.readDirectoryAsync(dir.uri),
data = null;
const props = await FileSystem.getInfoAsync(dir.uri + filename[0])
console.log(props)
try {
data = await FileSystem.readAsStringAsync(FileSystem.documentDirectory + 'avatar/profile');
}
catch (e) {
console.log(e);
}
console.log(data)
return data;
}

奇怪的是,const props = wait FileSystem.getInfoAsync(dir.uri + filename[0]) 正在打印:

Object {
"exists": 1,
"isDirectory": false,
"modificationTime": 1532930978,
"size": 399861,
"uri": "file:///var/mobile/Containers/Data/Application/9D3661AF-8EB5-49F5-A178-3ECA0F96BEEC/Documents/ExponentExperienceData/%2540anonymous%252FWAMS-1163fc3b-4484-44a2-9076-b4b71df1e55c/avatar/profile",
}

这应该表明图像已成功保存,但是 data = wait FileSystem.readAsStringAsync(dir.uri + filename[0]);data = wait FileSystem.readAsStringAsync(FileSystem .documentDirectory + 'avatar/profile') 会给我这个错误:

文件'file:///var/mobile/Containers/Data/Application/9D3661AF-8EB5-49F5-A178-3ECA0F96BEEC/Documents/ExponentExperienceData/%2540anonymous%252FWAMS-1163fc3b-4484-44a2-9076- b4b71df1e55c/avatar/profile'无法读取。

知道这是怎么发生的吗? FileSystem.readAsStringAsync() 甚至可以读取我从 ImagePicker 移动的文件吗?如果没有,我应该用什么来代替?

我正在 IOS 上尝试这个。

提前感谢您的帮助:smiley:

最佳答案

明白了!事实证明,我不必使用 FileSystem.moveAsync() 移动文件,我只需使用 FileSystem.writeAsStringAsync()base64 表示形式保存为普通字符串即可> 它解决了我的问题

关于reactjs - Expo FileSystem.readAsStringAsync 无法读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51588337/

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