使用react-native-image-picker从图库中删除图像

时间:2019-02-22 11:24:17

标签: react-native react-native-android

我创建了一个应用程序,该应用程序使用react-native-image-picker捕获照片并将其上传到Amazon s3。上传完成后,我想从图库中删除它们。当我搜索imagePicker API文档时,我了解了clean()方法。但是我没有在代码中使用它。

您能帮我解决这个问题吗?

我的代码是

import React, { Component } from "react";
import {
  Platform,
  StyleSheet,
  Alert,
  Text,
  TouchableOpacity,
  View,
  Picker,
  Animated,
  Easing,
  Image
} from "react-native";
import ImagePicker from "react-native-image-picker";
import { RNS3 } from "react-native-aws3";

export default class SecondScreen extends Component<Props> {
  constructor(props) {
    super(props);
    this.state = {
      file: "",
      saveImages: []
    };
  }

  takePic() {
    const options = {
          quality: 1.0,
          maxWidth: 50,
          maxHeight: 50,
      }
    ImagePicker.launchCamera(options,(responce)=>{
      const file = {
        uri: responce.uri,
        name: responce.fileName,
        method: "POST",
        path: responce.path,
        type: responce.type,
        notification: {
          enabled: true
        }
      };
      this.state.saveImages.push(file);
    });
  }
  _upload = saveImages => {
    const config = {
      keyPrefix: "uploads/",
      bucket: "s3merahkee",
      region: "us-east-2",
      accessKey: "***",
      secretKey: "***",
      successActionStatus: 201
    };

    this.state.saveImages.map(image => {
      RNS3.put(image, config).then(responce => {
        console.log(saveImages);
      });
    });

    //once after upload is done delete from the gallary
  };
  render() {
    return (
      <View style={styles.container}>
        <View style={styles.Camera}>
          <TouchableOpacity onPress={this.takePic.bind(this)}>
            <Text>Take Picture</Text>
          </TouchableOpacity>
        </View>
        <View style={styles.Send}>
          <TouchableOpacity onPress={() => this._upload()}>
            <Text>Send</Text>
          </TouchableOpacity>
        </View>
      </View>
    );
  }
}

1 个答案:

答案 0 :(得分:0)

安装react native react-native-fs这个帮助可以删除您的映像。

import { RNCamera } from 'react-native-camera';
import RNFS from 'react-native-fs';

takePhoto = async () => {
  const data = await this.camera.takePictureAsync();
  // Do what you need with the image and then…
  RNFS.unlink(data.uri); // Remove image from cache
}

也请参阅此https://github.com/itinance/react-native-fs/issues/34