反应原生图像选择器showimagepicker

时间:2017-05-16 06:48:08

标签: android npm react-native-android

我想使用react-native图像选择器拍摄图像, 我正在使用npm run android命令,但是当应用程序在expo中运行时,它会显示以下错误:

  

undefined不是对象(评估'imagepickerManager.showimagepicker')

当我使用expo时,项目index.android.js中没有react-native run-android,因此import React, {Component} from 'react'; import {Text, View, TouchableOpacity, Image} from 'react-native'; // var ImagePicker = require('react-native-image-picker'); import * as ImagePicker from 'react-native-image-picker'; // More info on all the options is below in the README...just some common use cases shown here var options = { title: 'Select Avatar', customButtons: [ {name: 'fb', title: 'Choose Photo from Facebook :'}, ], storageOptions: { skipBackup: true, path: 'images' } }; export default class App extends Component{ constructor(props){ super(props); this.state={ avatarSource:null } } render(){ let img=this.state.avatarSource == null?null: <Image source={this.state.avatarSource} style={{height:200, width:300}} /> return( <View> <Text>Welcome to Image Picker</Text> <TouchableOpacity onPress = {this.show.bind()} > <Text>Load Images</Text> </TouchableOpacity> {img} </View> ) } show(){ ImagePicker.showImagePicker(options, (response) => { if (response.didCancel) { console.log('User cancelled image picker'); } else if (response.error) { console.log('ImagePicker Error: ', response.error); } else if (response.customButton) { console.log('User tapped custom button please: ', response.customButton); } else { let source = { uri: response.uri }; // You can also display the image using data: // let source = { uri: 'data:image/jpeg;base64,' + response.data }; this.setState({ avatarSource: source }); } }); } } 命令无效。

有人可以指导我如何恢复此错误吗?

ERROR

package testcode;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;


public class ProducerDataBase implements Runnable {

    public String vardbtype;
    public String vardbserver;
    public String vardbuser;
    public String vardbpassword;
    public String vardbname;
    public int batchperiod2;

    public ProducerDataBase() {
        vardbtype = MYSQL;
        vardbserver = mysqltest;
        vardbuser = testadmin;
        vardbpassword = admin;
        vardbname = messages;
        batchperiod2 = 5000;
    }

    public void run(){
        ConnectionFactory factory = null;
        javax.jms.Connection connection = null;
        Session session = null;
        Destination destination = null;
        MessageProducer producer = null;
        Connection cs = null;
        try {
            factory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_BROKER_URL);
            connection = factory.createConnection();
            connection.start(); 
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            destination = session.createQueue("TestQueue");
            producer = session.createProducer(destination);
            if(vardbtype.equals("MYSQL")){
                Class.forName("com.mysql.jdbc.Driver");
                System.out.println("----------------------------");
                Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/"+ vardbserver, vardbuser, vardbpassword);
                while(true) {
                    Statement stmts = c.createStatement();
                    String sql = ("SELECT * FROM "+ vardbname);
                    ResultSet rss = stmts.executeQuery(sql);
                    while(rss.next()) {
                        String  message = rss.getString("MESSAGE");
                        System.out.println("Message = " + message);
                        TextMessage mssg = session.createTextMessage(message);
                        System.out.println("Sent: " + mssg.getText());
                        producer.send(mssg);
                    }
                    rss.close();
                    stmts.close();

                    Thread.sleep(batchperiod2);
                }
            }
        } catch(JMSException e) {
            e.printStackTrace();
        } catch(Exception e) {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
        } finally {
            if(session != null) {
                try {
                    session.close();
                } catch (JMSException ex) {
                    // ignore   
                }
            }
            if(connection != null) {
                try {
                    connection.close();
                } catch (JMSException ex) {
                    // ignore
                }
            }
        }
        System.out.println("----------------------------");
        System.out.println("Message sent successfully");
    }
}

3 个答案:

答案 0 :(得分:0)

我只是通过运行react-native link然后重建您的应用来修复此错误。它应该按预期工作。 :)

答案 1 :(得分:0)

我遇到了同样的问题。并以下列方式解决了它 -

  1. react-native link react-native-image-picker
  2. 运行react-native-run-ios和react-native run-android命令
  3. More description here

答案 2 :(得分:0)

从“react-native-image-picker”导入 * 作为 ImagePicker