首页 > 解决方案 > 如何在 React Native 中从 ImagePicker 中检索 blob/文件

问题描述

您好返回的对象ImagePicker from 'expo-image-picker'不包含任何文件,我不确定如何将返回的数据转换为 blob/文件,我是否必须将数据写入文件?创建第二个文件,如

 const data = new FormData()
 data.append('file', source)

反正我的代码

      let result = await ImagePicker.launchImageLibraryAsync({
        mediaTypes: ImagePicker.MediaTypeOptions.Images,
        allowsEditing: true,
        quality: 0.8
      })
      if (!result.cancelled) {
       console.log('Image ', source)
      }

当我控制台日志时我得到了这个

图像对象{“取消”:假,“高度”:750,“类型”:“图像”,“uri”:“file:///Users/macbookair/Library/Developer/CoreSimulator/Devices/3DE97E67-7232-4945 -BE1C-50C09FED6116/data/Containers/Data/Application/0C84C698-8C77-4D64-85AC-BFB0E88CBA0F/Library/Caches/ExponentExperienceData/%2540usfslk%252Fwillapprn/ImagePicker/19ED3307-DE54-4A09-A081-7DD3299.jpg","宽度“:1124,}

我可以添加base64选项并在响应时得到它,但是如果它已经存在,我为什么要将 base64 转换为图像?我的问题是 google firestore 无法识别发送的数据

FirebaseStorageError {
  "code_": "storage/invalid-argument",
  "message_": "Firebase Storage: Invalid argument in `put` at index 0: Expected Blob or File.",
  "name_": "FirebaseError",
  "serverResponse_": null,
}

标签: javascriptreactjsreact-native

解决方案


如果以后有人在寻找答案,这是一个很棒的功能

如何使用 react-native 和 firebase 在 expo 中上传图片的示例

 const response = await fetch(result.uri);
 const blob = await response.blob();
 this.uploadImage(blob)

推荐阅读