【问题标题】:react native CameraRollPicker upload image firebase null object reference反应原生 CameraRollPicker 上传图片 firebase 空对象引用
【发布时间】:2018-10-13 20:45:17
【问题描述】:

我正在尝试在 react-native 中使用 CameraRollPicker 将图像上传到 firebase。 第一次运行正常,但第二次进入 Imageupload 时出现此错误:

Attempt to invoke interface method 'java.lang.String 
com.facebook.react.bridge.ReadableMap.getString(java.lang.String)' on a null 
object reference
readAsText
FileReaderModule.java:43
invoke
Method.java
invoke
JavaMethodWrapper.java:372
invoke
JavaModuleWrapper.java:160
run
NativeRunnable.java
handleCallback
Handler.java:790
dispatchMessage
Handler.java:99
dispatchMessage
MessageQueueThreadHandler.java:29
loop
Looper.java:164
run
MessageQueueThreadImpl.java:192
run
Thread.java:764

我尝试过的:

import React, { Component } from 'react';
import {
 AppRegistry,
 StyleSheet,
 Text,
 View
} from 'react-native';
import firebase from 'firebase';
import { Actions } from 'react-native-router-flux';
import RNFetchBlob from 'react-native-fetch-blob';
import CameraRollPicker from 'react-native-camera-roll-picker';

export default class ImageUpload extends Component {


 getSelectedImages = (selectedImages, currentImage) => {

   const image = currentImage.uri;
   const Blob = RNFetchBlob.polyfill.Blob;
   const fs = RNFetchBlob.fs;
  window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
  window.Blob = Blob;


   let uploadBlob = null;
   const imageRef = firebase.storage().ref('pictures').child('test.jpg');
   let mime = 'image/jpg';
   fs.readFile(image, 'base64')
   .then((data) => {
    console.log(data);
    return Blob.build(data, { type: `${mime};BASE64` });
   }) 
   .then((blob) => {
     uploadBlob = blob;
     console.log(uploadBlob);
     console.log(blob);
     return imageRef.put(blob, { contentType: mime });
   })
   .then(() => {
     uploadBlob.close();
     return imageRef.getDownloadURL();
   })
   .then((url) => {
     // URL of the image uploaded on Firebase storage
     console.log(url);
     Actions.subjects();

   })
   .catch((error) => {
     console.log(error);

   });

}

render() {
 return (
   <View style={styles.gallery}>
<CameraRollPicker selected={[]} maximum={1} callback= 
{this.getSelectedImages} />         <Text>
       Image Gallery
     </Text>
   </View>
  );
   }
 }

 const styles = StyleSheet.create({
   container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  gallery: {
    flex: 1,
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  }
});

我如何从我的主文件中调用 ImageUpload:

<View style={{ flex: 1, backgroundColor: '#4c7794', justifyContent: 'space-between' }}>
<ImageUpload />
  <NavBar />
</View>

空对象引用可能在哪里?你有什么建议吗?为什么它第一次起作用?

【问题讨论】:

    标签: android firebase react-native jsx


    【解决方案1】:

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

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

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

    还有:

    如果您有使用 whatwg-fetch 的现有代码,现在您没有 要在 0.9.0 之后更改现有代码,只需使用 fetch 替换。这 Official fetch 和 fetch replacement 的区别在于, 官方 fetch 使用包装 XMLHttpRequest 的 WHATWG-fetch js 库 引擎盖下的 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

    【讨论】:

      猜你喜欢
      • 2020-11-16
      • 2018-01-31
      • 2022-09-30
      • 2021-01-05
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多