I'm trying to use expo image picker to send image file to an API in form data but when I tried to use console.log to see the result, it does not show the image name. It shows the uri, type, width and height. How do I get the image name?
const pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
aspect: [4, 3],
quality: 1,
base64: false,
});
console.log(result);
if (!result.cancelled) {
setImage(result.uri);
}
};
I'm trying to use expo image picker to send image file to an API in form data but when I tried to use console.log to see the result, it does not show the image name. It shows the uri, type, width and height. How do I get the image name?
const pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
aspect: [4, 3],
quality: 1,
base64: false,
});
console.log(result);
if (!result.cancelled) {
setImage(result.uri);
}
};
Share
Improve this question
edited Jul 5, 2022 at 4:17
Nomel
asked Jul 5, 2022 at 3:18
NomelNomel
1195 silver badges17 bronze badges
3 Answers
Reset to default 5Parse filename from uri
if (!result.cancelled) {
setImage(result.uri);
const fileName = result.uri.split('/').pop();
const fileType = fileName.split('.').pop();
console.log(fileName, fileType);
}
You can use expo-document-picker with {type:'image/*'}
as DocumentPickerOptions which gives picked images details including name, type, uri etc... which is better alternate for expo-image-picker
From what I read in the Image Picker docs, the uri will be stored under the assets object:
const pickImage = async () => {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.Images,
aspect: [4, 3],
quality: 1,
base64: false,
});
// look at the keys, assets should be one
console.log(Object.keys(result));
if (!result.cancelled) {
// It appears that assets is an array?
setImage(result.assets[0].uri);
}
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745292818a4620963.html
评论列表(0条)