我正在尝试在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>
null对象引用在哪里?你有什么建议吗?为什么它第一次起作用?
答案 0 :(得分:0)
我遇到了同样的错误。解决方案是按照官方文档中的说明进行“获取替换”操作:
由于我们未实现FileReader polyfill,因此您可能会遇到 在某些情况下会出现此错误。
如果您在调试模式下使用Blob polyfill遇到此问题,请尝试 用获取替换替换window.fetch应该可以解决它。
并且:
如果您已有使用whatwg-fetch的代码,则现在没有了 要在0.9.0之后更改现有代码,只需使用提取替换。的 正式获取和替换获取之间的区别在于, 官方提取使用包装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()