Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete images from gallery using react-native-image-picker

Tags:

react-native

I have created an app which captures the photo using react-native-image-picker and uploads to amazon s3. Once the upload is done I want to delete them from the gallery. When I search imagePicker API documention I got to know about clean() method. But I did not get it how to use it in my code.

Can you help me to solve this?

My code is,

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>
    );
  }
}
like image 887
Vidya Kabber Avatar asked Dec 03 '25 03:12

Vidya Kabber


1 Answers

Install react native react-native-fs this help to remove your image.

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
}

also refer this https://github.com/itinance/react-native-fs/issues/34

like image 136
Mitesh jadav Avatar answered Dec 06 '25 11:12

Mitesh jadav



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!