我有以下设置,当用户使用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 = await 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 = await FileSystem.readAsStringAsync(dir.uri + filename[0]);
或
data = await FileSystem.readAsStringAsync(FileSystem.documentDirectory + 'avatar/profile')
会给我这个错误:
File 'file:///var/mobile/Containers/Data/Application/9D3661AF-8EB5-49F5-A178-3ECA0F96BEEC/Documents/ExponentExperienceData/%2540anonymous%252FWAMS-1163fc3b-4484-44a2-9076-b4b71df1e55c/avatar/profile' could not be read.
你知道怎么会这样吗?可以
FileSystem.readAsStringAsync()
甚至读取我移动的文件
ImagePicker
?如果没有,我应该用什么来代替?
我正在iOS上尝试。
提前感谢您的帮助:笑脸: