首页 文章

尝试在空对象引用上调用接口方法java.lang.String com.facebook.react.bridge.ReadableMap ...

提问于
浏览
0

将图像上传到firebase存储后,我收到此错误 . 我正在使用“react-native”:“0.55.4”,“react-native-fetch-blob”:“^ 0.10.8”,“react-native-image-picker”:“^ 0.26.10”,“ firebase“:”^ 5.0.4“,

这是我上传图片的代码 .

// Prepare Blob support
const Blob = RNFetchBlob.polyfill.Blob;

const fs = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;

uploadImage = (uri, imageName, mime = "image/jpg") => {
    return new Promise((resolve, reject) => {
      const uploadUri =
        Platform.OS === "ios" ? uri.replace("file://", "") : uri;
      let uploadBlob = null;
      const imageRef = db
        .storage()
        .ref("images/")
        .child(imageName);

  fs.readFile(uploadUri, "base64")
    .then(data => {
      return Blob.build(data, { type: `${mime};BASE64` });
    })
    .then(blob => {
      uploadBlob = blob;
      alert("blob is " + JSON.stringify(blob));
      return imageRef.put(blob, { contentType: mime });
    })
    .then(() => {
      uploadBlob.close();
      return imageRef.getDownloadURL();
    })
    .then(url => {
      resolve(url);
    })
    .catch(error => {
      reject(error);
    });
});};

尝试在空对象引用上调用接口方法'java.lang.String com.facebook.react.bridge.ReadableMap.getString(java.lang.String)'readAsText FileReaderModule.java:43调用Method.java调用JavaMethodWrapper.java: 372调用JavaModuleWrapper.java:160运行NativeRunnable.java handleCallback Handler.java:790 dispatchMessage Handler.java:99 dispatchMessage MessageQueueThreadHandler.java:29循环Looper.java:164运行MessageQueueThreadImpl.java:192运行Thread.java:764

1 回答

  • -2

    我遇到了同样的错误 . 正如官方文档所解释的那样,解决方案是进行“获取替换”:

    由于我们没有实现FileReader polyfill,因此在某些情况下可能会遇到此错误 . 如果您在调试模式下遇到Blob polyfill的这个问题,请尝试使用fetch替换window.fetch来修复它 .

    和:

    如果你有使用whatwg-fetch的现有代码,现在你不必在0.9.0之后更改现有代码,只需使用fetch replacement . 官方fetch和fetch替换之间的区别在于,官方fetch使用WHATWG-fetch js库来封装XMLHttpRequest polyfill,我们的实现只包含了RNFetchBlob.fetch .

    基本上,您只需将其添加到您的代码中,就在 window.Blob = Blob; 行下方:

    const Fetch = RNFetchBlob.polyfill.Fetch
    // replace built-in fetch
    window.fetch = new Fetch({
        // enable this option so that the response data conversion handled automatically
        auto : true,
        // when receiving response data, the module will match its Content-Type header
        // with strings in this array. If it contains any one of string in this array, 
        // the response body will be considered as binary data and the data will be stored
        // in file system instead of in memory.
        // By default, it only store response data to file system when Content-Type 
        // contains string `application/octet`.
        binaryContentTypes : [
            'image/',
            'video/',
            'audio/',
            'foo/',
        ]
    }).build()
    

    文件:https://github.com/wkh237/react-native-fetch-blob/wiki/Trouble-Shooting#failed-to-execute-readastext-on-filereader

相关问题