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

dan*_*han 3 url storage image firebase react-native

将图像上传到 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);
    });
});};
Run Code Online (Sandbox Code Playgroud)

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

小智 7

我遇到了同样的错误。解决方案是按照官方文档的说明进行“获取替换”:

由于我们没有实现 FileReader polyfill,因此在某些情况下您可能会遇到此错误。

如果您在调试模式下遇到 Blob polyfill 的这个问题,请尝试用 fetch 替换替换 window.fetch 应该可以解决它。

和:

如果您有使用 whatwg-fetch 的现有代码,那么现在您不必更改 0.9.0 之后的现有代码,只需使用 fetch 替换即可。Official fetch 和 fetch replacement 的区别在于,official 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()
Run Code Online (Sandbox Code Playgroud)

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