3

我根据文档安装了 react-native-image-picker。当我尝试从手机中选择图像时(按下按钮后),模拟器给了我这个错误 - null is not an object (evaluating 'ImagePickerManager.showImagePicker')

我的 React native 的版本是 0.59.8

图像选择器的版本是 0.28.0

这是代码-

import React, { Component } from 'react';
import {
  StyleSheet,
  Text,
  View,
  Image,
  Button
} from 'react-native';
import ImagePicker from "react-native-image-picker";

export default class App extends Component {

  state = {
    pickedImage: null
  }

  reset = () => {
    this.setState({
      pickedImage: null
    });
  }




pickImageHandler = () => {
    ImagePicker.showImagePicker({title: "Pick an Image", maxWidth: 800, maxHeight: 600}, res => {
      if (res.didCancel) {
        console.log("User cancelled!");
      } else if (res.error) {
        console.log("Error", res.error);
      } else {
        this.setState({
          pickedImage: { uri: res.uri }
        });

      }
    });
  }



       resetHandler = () =>{
            this.reset();
          }

  render() {
    return (
      <View style={styles.container}>
      <Text style={styles.textStyle}>Pick Image From Camera and Gallery </Text>
        <View style={styles.placeholder}>
          <Image source={this.state.pickedImage} style={styles.previewImage} />
        </View>
        <View style={styles.button}>

          <Button title="Pick Image" onPress={this.pickImageHandler} />

          <Button title="Reset" onPress={this.resetHandler} />

         </View>
      </View>
    );
  }
}

4

4 回答 4

2

我将图像选择器的版本降级为28.0.0然后重建了应用程序。现在它正在工作。

于 2019-08-19T06:04:09.260 回答
1

我认为当前版本存在错误。我遇到过这种情况,我所做的是卸载 image-picker 包上的当前版本,并安装了版本@1.1.0

或者您可以简单地降级您当前的版本。

  1. npm uninstall react-native-image-picker

  2. npm install react-native-image-picker@1.1.0 --save

  3. react-native link react-native-image-picker

  4. cd ios

  5. pod install

  6. 光盘..

停止并重新运行您的 Packager 或 Bundler

然后重置你的缓存

npm start --reset-cache

于 2019-12-11T13:07:41.547 回答
0

不是ImagePickerManager你可以使用ImagePicker

例子

import ImagePicker from 'react-native-image-picker';

// More info on all the options is below in the API Reference... just some common use cases shown here
const options = {
  title: 'Select Avatar',
  customButtons: [{ name: 'fb', title: 'Choose Photo from Facebook' }],
  storageOptions: {
    skipBackup: true,
    path: 'images',
  },
};

/**
 * The first arg is the options object for customization (it can also be null or omitted for default options),
 * The second arg is the callback which sends object: response (more info in the API Reference)
 */
ImagePicker.showImagePicker(options, (response) => {
  console.log('Response = ', 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: ', response.customButton);
  } else {
    const source = { uri: response.uri };

    // You can also display the image using data:
    // const source = { uri: 'data:image/jpeg;base64,' + response.data };

    this.setState({
      avatarSource: source,
    });
  }
});
于 2019-08-05T13:02:28.727 回答
0

在 android studio 中构建项目,如果遇到如下错误:

this.options.saveToPhotos && Build.VERSION.SDK_INT <= Build.VERSION_CODES.P && ....

在你的projectName/android/build.gradle更新这些

   ext {
     buildToolsVersion = "29.0.2"
     minSdkVersion = 21
     compileSdkVersion = 29
     targetSdkVersion = 29
}

并更新这个

  afterEvaluate {
        project -> if (project.hasProperty("android")) {
            android {
                compileSdkVersion 29
                buildToolsVersion '29.0.2'
            }
        }
    }
于 2021-02-09T19:22:47.970 回答