Search code examples
javascriptreact-nativefile-uploadexpo

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


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

Solution

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