1:通过函数回调,传递一个string或者一个object 不能传递json如果想传递json格式 可以将json转成string 到react端再解析会json
2:通过promise 使用async,await 传递一个map
3:通过事件native端可以发送参数到js 传递一个map
1:代码:
/**
* index.android.js
*/
'use strict';
import React, {
AppRegistry,
Component,
StyleSheet,
Text,
Image,
TouchableHighlight,
View,
NativeModules
} from 'react-native';
var path = require('./Lighthouse.jpg');
var QRFromAlbum = require('./mymodule/QRFromAlbum');
class TestQRAndroidExample extends React.Component{
constructor(props) {
super(props);
this.state = {
torchMode: 'off',
cameraType: 'back',
flag:false,
Imagepath:null,
};
}
onClick(){
<strong><span style="font-size:18px;"> QRFromAlbum.chooseImage((res)=>{
var json = JSON.parse(res);
var path = json.imagepath;//得到图片的路径
this.setState({
flag:true,
Imagepath:path,
});</span></strong>
});
}
render() {
if(this.state.flag){
console.log("path:"+this.state.Imagepath);
return (<View style={styles.main}>
<Image style={styles.containerImageView}
source={
{uri:this.state.Imagepath}}>
</Image>
</View>
);
}else{
return (<View style={styles.main}>
<TouchableHighlight style={styles.button} underlayColor='#99d9fe' onPress={()=>{this.onClick();}}>
<Text style={styles.buttontext}>打开相册 </Text>
</TouchableHighlight>
</View>
);
}
}
};
var styles = StyleSheet.create({
containerTextView: {
flex: 1,
},
containerImageView:{
flex: 1,
position:'relative',
width:300,
height:300,
flexDirection:'row',
justifyContent:'center',
},
text:{
position:'relative',
color:'white',
flex:1,
bottom:160,
left:90,
height:60,
fontSize: 15,
fontWeight: 'bold',
flexDirection:'row',
justifyContent:'center',
},
button:{
position:'relative',
flex:1,
backgroundColor:'#48BBEC',
bottom:170,
left:100,
height:35,
flexDirection:'row',
justifyContent:'center',
borderRadius:10,
},
buttontext:{
flex:1,
fontSize:15,
color:'white',
alignSelf:'center',
},
main:{
flex:1,
position:'absolute',
},
});
AppRegistry.registerComponent('TestUI', () => TestQRAndroidExample);
Java代码:
module部分
package com.nativemodule;
import android.app.Activity;
import android.content.Intent;
i