Home > Net >  React Native How to get the image name when using image picker?
React Native How to get the image name when using image picker?

Time:07-05

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);
        }
    };

CodePudding user response:

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

CodePudding user response:

Parse filename from uri

if (!result.cancelled) {
  setImage(result.uri);

  const fileName = result.uri.split('/').pop();
  const fileType = fileName.split('.').pop();

  console.log(fileName, fileType);
}

CodePudding user response:

just wanted to know why you need ImagePicker we can do it with input tag in html

  • Related