From 3ebcaaa5de65705151f9a3bd8656e02e4e8ad070 Mon Sep 17 00:00:00 2001 From: analuizaff Date: Tue, 29 Dec 2020 18:37:40 -0300 Subject: [PATCH] Picking image from camera or gallery --- src/app/components/ImageInput.js | 103 ++++++++++++++++++++++++----- src/app/components/LaunchCamera.js | 53 ++++++++------- src/package.json | 1 + 3 files changed, 115 insertions(+), 42 deletions(-) diff --git a/src/app/components/ImageInput.js b/src/app/components/ImageInput.js index 6c1d674..689135c 100644 --- a/src/app/components/ImageInput.js +++ b/src/app/components/ImageInput.js @@ -5,32 +5,44 @@ import { Image, TouchableWithoutFeedback, Alert, + Text, + Modal, + TouchableHighlight, } from "react-native"; import { MaterialCommunityIcons } from "@expo/vector-icons"; import * as ImagePicker from "expo-image-picker"; +import { Dialog } from 'react-native-simple-dialogs'; import colors from "../config/colors"; +import { useState } from "react/cjs/react.development"; function ImageInput({ imageUri, onChangeImage }) { useEffect(() => { requestPermission(); + requestPermissionCamera(); }, []); + const [modalVisible, setModalVisible] = useState(false); const requestPermission = async () => { const { granted } = await ImagePicker.requestCameraRollPermissionsAsync(); - if (!granted) alert("You need to enable permission to access the library."); + if (!granted) alert("Você precisa habilitar permissão para acessar a biblioteca."); + }; + const requestPermissionCamera = async () => { + const { granted } = await ImagePicker.requestCameraPermissionsAsync(); + if (!granted) alert("Você precisa habilitar permissão para acessar a câmera."); }; const handlePress = () => { - if (!imageUri) selectImage(); + if (!imageUri) setModalVisible(true); else - Alert.alert("Delete", "Are you sure you want to delete this image?", [ - { text: "Yes", onPress: () => onChangeImage(null) }, - { text: "No" }, + Alert.alert("Deletar", "Deseja deletar esta imagem?", [ + { text: "Sim", onPress: () => onChangeImage(null) }, + { text: "Não" }, ]); }; const selectImage = async () => { + setModalVisible(false); try { const result = await ImagePicker.launchImageLibraryAsync({ mediaTypes: ImagePicker.MediaTypeOptions.Images, @@ -38,23 +50,55 @@ function ImageInput({ imageUri, onChangeImage }) { }); if (!result.cancelled) onChangeImage(result.uri); } catch (error) { - console.log("Error reading an image", error); + console.log("Erro ao ler imagem", error); + } + }; + + const takePicture = async () => { + setModalVisible(false); + try { + const result = await ImagePicker.launchCameraAsync({ + allowsEditing: false, + }); + if (!result.cancelled) onChangeImage(result.uri) + } catch (error) { + console.log("Erro ao ler imagem", error); } }; + return ( - - - {!imageUri && ( - - )} - {imageUri && } + + + + {!imageUri && ( + + )} + {imageUri && } + + + + + { + Alert.alert('Modal has been closed.'); + }}> + + + Câmera + Galeria + + + - + ); } @@ -73,6 +117,31 @@ const styles = StyleSheet.create({ height: "100%", width: "100%", }, + centeredView: { + flex: 1, + justifyContent: 'center', + alignItems: 'center', + marginTop: 22, + }, + modalView: { + margin: 20, + backgroundColor: 'white', + borderRadius: 20, + padding: 35, + alignItems: 'center', + shadowColor: '#000', + shadowOffset: { + width: 0, + height: 2, + }, + shadowOpacity: 0.25, + shadowRadius: 3.84, + elevation: 5, + }, + modalText: { + marginBottom: 15, + textAlign: 'center', + }, }); export default ImageInput; diff --git a/src/app/components/LaunchCamera.js b/src/app/components/LaunchCamera.js index d7bb02c..c9b8072 100644 --- a/src/app/components/LaunchCamera.js +++ b/src/app/components/LaunchCamera.js @@ -1,17 +1,20 @@ import React from "react"; -import { StyleSheet, Text, View, SafeAreaView, Image, Button } from "react-native"; +import { StyleSheet, Text, View, SafeAreaView, Image, Button, TouchableWithoutFeedback } from "react-native"; import * as ImagePicker from "expo-image-picker"; import { useState, useEffect } from "react"; import * as Permissions from 'expo-permissions'; +import colors from "../config/colors"; +import { MaterialCommunityIcons } from "@expo/vector-icons"; + function LaunchCamera() { - const [image, setImage] = useState(); + const [imageUri, setImageUri] = useState(null); const takePicture = async () => { await Permissions.askAsync(Permissions.CAMERA); const { cancelled, uri } = await ImagePicker.launchCameraAsync({ allowsEditing: false, }); - setImage({ image: uri }); + setImageUri(uri); }; const handlePress = () => { @@ -19,36 +22,36 @@ function LaunchCamera() { }; return ( - - - -