React native 将文件写入目录错误-Expo文件系统
我真的很难找到我在这方面的错误。我正在尝试将拾取的(ImagePicker)图像从缓存移动到名为React native 将文件写入目录错误-Expo文件系统,react-native,expo,React Native,Expo,我真的很难找到我在这方面的错误。我正在尝试将拾取的(ImagePicker)图像从缓存移动到名为images/的app scope目录文件夹。我使用文件系统创建了一个目录images/。MakeDirectorySync,但在尝试将拾取的图像移动到此目录时,出现了一个错误。有人能帮帮我吗我真的很挣扎 预期结果 [Unhandled promise rejection: Error: File 'file:///var/mobile/Containers/Data/Application/318C
images/
的app scope目录文件夹。我使用文件系统创建了一个目录images/
。MakeDirectorySync,但在尝试将拾取的图像移动到此目录时,出现了一个错误。有人能帮帮我吗我真的很挣扎
预期结果
[Unhandled promise rejection: Error: File 'file:///var/mobile/Containers/Data/Application/318CFCE4-16DF-44DD-92B3-39DECA61EA14/Library/Caches/ExponentExperienceData/%2540user%252FtestApp/ImagePicker/ECD218AE-3DD3-429F-B1F5-469DA1AC661C.jpg' could not be moved to
'file:///var/mobile/Containers/Data/Application/318CFCE4-16DF-44DD-92B3-39DECA61EA14/Documents/ExponentExperienceData/%2540user%252FtestApp/images/ECD218AE-3DD3-429F-B1F5-469DA1AC661C.jpg/'.]
图像成功移动到images/
目录
实际结果
[Unhandled promise rejection: Error: File 'file:///var/mobile/Containers/Data/Application/318CFCE4-16DF-44DD-92B3-39DECA61EA14/Library/Caches/ExponentExperienceData/%2540user%252FtestApp/ImagePicker/ECD218AE-3DD3-429F-B1F5-469DA1AC661C.jpg' could not be moved to
'file:///var/mobile/Containers/Data/Application/318CFCE4-16DF-44DD-92B3-39DECA61EA14/Documents/ExponentExperienceData/%2540user%252FtestApp/images/ECD218AE-3DD3-429F-B1F5-469DA1AC661C.jpg/'.]
这是我的代码:
import React, { useEffect, useState } from "react";
import {Text,View,TouchableOpacity,Alert,} from "react-native";
import * as ImagePicker from "expo-image-picker";
import * as Permissions from "expo-permissions";
import * as FileSystem from "expo-file-system";
const ImageCard = (props) => {
const { handlePickedImage } = props;
const [image, setImage] = useState("");
// Create any app folders that don't already exist
export const checkAndCreateFolder = async folder_path => {
const folder_info = await FileSystem.getInfoAsync(folder_path);
if (!Boolean(folder_info.exists)) {
// Create folder
console.log("checkAndCreateFolder: Making " + folder_path);
try {
await FileSystem.makeDirectoryAsync(folder_path, {
intermediates: true
});
} catch (error) {
// Report folder creation error, include the folder existence before and now
const new_folder_info = await FileSystem.getInfoAsync(folder_path);
const debug = `checkAndCreateFolder: ${
error.message
} old:${JSON.stringify(folder_info)} new:${JSON.stringify(
new_folder_info
)}`;
console.log(debug);
}
}
};
const veryfiyPermissons = async () => {
const result = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (result.status !== "granted") {
Alert.alert(
"Insufficient permissions",
"You need to grant permissions to access Camera Roll",
[{ text: "Okay" }]
);
return false;
}
return true;
};
const selectImageHandler = async () => {
const hasPermisson = await veryfiyPermissons();
if (!hasPermisson) {
return;
}
const image = await ImagePicker.launchImageLibraryAsync({
quality: 0.5,
});
if (image.cancelled) {
randomImage;
} else {
let localUri = image.uri;
let localUriNamePart = localUri.split("/");
const fileName = localUriNamePart[localUriNamePart.length - 1];
const images_folder = `${FileSystem.documentDirectory + 'images/'}`
checkAndCreateFolder(images_folder);
const setTheFile = `${images_folder + `${fileName}/`}`
await FileSystem.moveAsync({
from: localUri,
to: newLocation
}).then((i) => {
setImage(setTheFile);
handlePickedImage(setTheFile);
})
}
};
return (
<View>
<TouchableOpacity onPress={selectImageHandler}>
<Text>Add Photo</Text>
</TouchableOpacity>
</View>
);
};
export default ImageCard;
import React,{useffect,useState}来自“React”;
从“react native”导入{文本、视图、TouchableOpacity、警报、};
从“世博会图像采集器”导入*作为图像采集器;
从“expo Permissions”导入*作为权限;
从“expo文件系统”导入*作为文件系统;
常量图像卡=(道具)=>{
const{handlePickedImage}=props;
const[image,setImage]=useState(“”);
//创建任何尚不存在的应用程序文件夹
导出常量checkAndCreateFolder=异步文件夹\u路径=>{
const folder_info=wait FileSystem.getInfoAsync(文件夹路径);
如果(!Boolean(文件夹信息存在)){
//创建文件夹
log(“checkAndCreateFolder:Making”+文件夹路径);
试一试{
等待FileSystem.makeDirectorySync(文件夹路径{
中间产物:正确
});
}捕获(错误){
//报表文件夹创建错误,包括以前和现在存在的文件夹
const new\u folder\u info=wait FileSystem.getInfoAsync(文件夹路径);
const debug=`checkAndCreateFolder:${
错误消息
}旧:${JSON.stringify(文件夹_info)}新:${JSON.stringify(
新文件夹信息
)}`;
日志(调试);
}
}
};
const veryfiyPermissons=async()=>{
const result=wait Permissions.askAsync(Permissions.CAMERA_ROLL);
如果(result.status!=“已授予”){
警惕,警惕(
“权限不足”,
“您需要授予访问相机卷的权限”,
[{文本:“好的”}]
);
返回false;
}
返回true;
};
const selectImageHandler=async()=>{
const hasPermisson=wait veryfiypermisson();
如果(!hasPermisson){
返回;
}
const image=wait ImagePicker.launchImageLibraryAsync({
质量:0.5,
});
如果(图像已取消){
随机图像;
}否则{
让localUri=image.uri;
让localUriNamePart=localUri.split(“/”);
const fileName=localUriNamePart[localUriNamePart.length-1];
const images_folder=`${FileSystem.documentDirectory+'images/'}`
checkAndCreateFolder(图像\u文件夹);
常量setTheFile=`${images\u folder+`${fileName}/`}`
等待FileSystem.moveAsync({
发件人:localUri,
致:新址
})。然后((i)=>{
设置图像(设置文件);
handlePickedImage(抵销文件);
})
}
};
返回(
添加照片
);
};
导出默认图像卡;