首页 > 解决方案 > expo-image-picker 上传多张图片

问题描述

大家好,我正在学习 react-native 中的 expo-image-picker,当我上传一张图片时一切都很好,但我想上传多张图片。但如果有人知道该怎么做,它就不起作用,请帮忙。

这是我尝试检查一次的代码..

_pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [9, 16],
      quality: 1.0,
      allowsMultipleSelection: true,
    });
    console.log(result);
    if (!result.cancelled) {
      this.setState({ image: result.uri });
    }
  };



  getPermissionAsync = async () => {
    if (Constants.platform.android) {
      const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
      status = await Permissions.getAsync(Permissions.CAMERA);
      if (status !== 'granted') {
        alert('Sorry, we need camera roll permissions to make this work!');
      }
    }
  }


import React, { Component } from 'react';
import {
  Image, Platform, StyleSheet, Text, View, TextInput, Button, TouchableHighlight, Alert,
  TouchableOpacity, ScrollView, ColorPropType, FlatList, SectionList, Dimensions, AsyncStorage,
  Keyboard, Modal, NativeModules, SafeAreaView, StatusBar,
} from 'react-native';


import PropTypes from 'prop-types'


// import { Video } from 'expo-av';

import { Camera } from 'expo-camera';
import * as ImagePicker from 'expo-image-picker';
import Constants from 'expo-constants';
import * as Permissions from 'expo-permissions';


export default class AddPostScreen extends React.Component {  
  constructor(props) {
    super(props);
    this.state = {
      loading: true,
      image: null,
      images: [],
    };

  }


  async componentDidMount() {
    this.get_Id();
    this.getPermissionAsync();
    this.get_PermissionAsync();

  }



  _pickCamera = async () => {
    let result = await await ImagePicker.launchCameraAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: false,
      aspect: [4, 3],
      quality: 1.0,
     allowsMultipleSelection: true,
    });
    console.log(result);
    if (!result.cancelled) {
      this.setState({ image: result.uri });
    }
  };



  get_PermissionAsync = async () => {
    if (Constants.platform.android) {
      const { status } = await Permissions.getAsync(Permissions.CAMERA);
      if (status !== 'granted') {
        alert('Sorry, we need camera roll permissions to make this work!');
      }
    }
  }


 _pickImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.All,
      allowsEditing: true,
      aspect: [9, 16],
      quality: 1.0,
      allowsMultipleSelection: true,
    });
    console.log(result);
    if (!result.cancelled) {
      this.setState({ image: result.uri });
    }
  };



  getPermissionAsync = async () => {
    if (Constants.platform.android) {
      const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
      status = await Permissions.getAsync(Permissions.CAMERA);
      if (status !== 'granted') {
        alert('Sorry, we need camera roll permissions to make this work!');
      }
    }
  }


  render() {

    const { image, images } = this.state;

      return (


         <View style={styles.container}>
          <View style={styles.ifContainer}>

            <View>
              <Image style={styles.imageView} source={{ uri: this.state.image }} />
              <View style={styles.row}>
                <Button style={styles.button} onPress={this._pickImage} title="Open Gallery" />
                <Button style={styles.button} onPress={this._pickCamera} title="Open Camera" />
              </View>
            </View>

          </View>
        </View >

      );

  }// render ends


} // class Ends




app.json 中的权限

"android": {
      "package": "com.srastr.howtags",
      "versionCode": 6,
      "googleServicesFile": "./google-services.json",
      "permissions": [
        "CAMERA",
        "CAMERA_ROLL",
        "READ_INTERNAL_STORAGE",
        "READ_EXTERNAL_STORAGE",
        "WRITE_EXTERNAL_STORAGE",
        "RECORD_AUDIO"
      ]
    }

如果有人知道使用 expo-imagepicker 上传多个文件的解决方案,请告诉我..

标签: react-nativeexpo

解决方案


使用 Expo-image-picker-multiple

https://www.npmjs.com/package/expo-image-picker-multiple

https://www.npmjs.com/package/expo-image-picker-multiple

推荐阅读