最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - React Native How to get the image name when using image picker? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 5

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

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);
  }
};
发布评论

评论列表(0)

  1. 暂无评论