我正在嘗試從真實設備上拍照并使用 expo 將其保存在新位置expo-file-system。但我收到一個錯誤Error: FileSystem.moveAsync needs a to path.
我的代碼:
const fileMoveHandler = async () => {
const fileName = selectedImage.split("/").pop();
console.log("FileSystem:", FileSystem.documentDirectory fileName);
setNewPathImage(FileSystem.documentDirectory fileName);
console.log("newPathImage", newPathImage);
try {
await FileSystem.moveAsync({
from: selectedImage,
to: newPathImage,
});
} catch (err) {
console.log("Error:", err);
Alert.alert("Can't move this file.", "Try again!", [{ text: "Okay" }]);
}
};
控制臺日志:
FileSystem: file:///data/user/0/host.exp.exponent/files/ExperienceData/******/3061c494-6c3b-4307-abb3-a0d8681d2bb3.jpg
newPathImage undefined
Error: [Error: `FileSystem.moveAsync` needs a `to` path.]
當我變得newPathImage未定義時,這就是為什么它給我錯誤但我不知道為什么我變得未定義?
如何設定影像路徑的狀態?
uj5u.com熱心網友回復:
一定是因為它setState()是異步的,所以當你嘗試設定它to時newPathImage它仍然是未定義的。如果您改為執行以下操作,它應該可以作業:
const fileMoveHandler = async () => {
const fileName = selectedImage.split("/").pop();
const newPathImage = FileSystem.documentDirectory fileName
console.log("FileSystem:", newPathImage);
setNewPathImage(newPathImage);
console.log("newPathImage", newPathImage);
try {
await FileSystem.moveAsync({
from: selectedImage,
to: newPathImage,
});
} catch (err) {
console.log("Error:", err);
Alert.alert("Can't move this file.", "Try again!", [{ text: "Okay" }]);
}
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/418815.html
標籤:
